Come utilizzare la variabile avviata in query SQL?
-
26-12-2019 - |
Domanda
Ho fatto una query per ottenere il valore massimo di una certa colonna
sSQLmax = "SELECT MAX([tablename]!rowname) as MaxNum FROM [tablename]"
.
Voglio usare il valore di MAXNUM.L'ho provato in un msgbox e mi ha dato un valore vuoto.Ho provato tutti questi:
MsgBox "MaxNum" & MaxNum
MsgBox "MaxNum" & sSQLMAX.MaxNum
MsgBox "MaxNum" & sSQLMAX!MaxNum
.
Più del codice
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(sSQLmax)
.
Ma nessuno funziona, sono sicuro che è semplice ma mi sfuoca
Soluzione
Con il codice si sta solo impostando una variabile in una stringa.Non stai effettivamente facendo nulla con esso.Questo è probabilmente più di ciò che stai cercando:
Dim db As DAO.Database, rst As DAO.Recordset, sSQLmax As String
sSQLmax = "SELECT MAX([tablename].rowname) as MaxNum FROM [tablename]"
Set db = CurrentDb
Set rst = db.OpenRecordset(sSQLmax, dbOpenDynaset)
If (rst.RecordCount <> 0) Then
rst.MoveFirst
MsgBox "MaxNumber: " & rst.Fields("rowname")
End If
. Altri suggerimenti
Prova la funzione DMAX
Dim result as String
result = DMax("[COLUMN NAME]", "[TABLE NAME]")
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow