Condividi tramite

estrarre valore da SQL

Anonimo
2014-02-07T13:04:31+00:00

Ho questo codice:

Dim A as Date, B as Integer

DoCmd.RunSQL "SELECT Last(TXF2.DFAT) AS UDF, Max(TXF2.NFAT) AS UNF FROM TXF2;"

Come posso assegnare alle due variabili i valori UDF ed UNF?

Se scrivo A = UDF e B = UNF non me li accetta.

Grazie.

Microsoft 365 e Office | Access | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

Risposta accettata dall'autore della domanda

Anonimo
2014-02-07T13:32:07+00:00

Ciao,

  1. La tua riga di codice ti deve dare un errore perché il commando RunSQL richiede una query di azione o definizione non una query di selezione.
  2. Per prendere i valori dalla query puoi usare un recordset:

Dim db As DAO.Database

Dim rs As DAO.Recordset

Dim A as Date

Dim B as Integer

Dim strSQL As String

strSQL = "SELECT Last(DFAT) AS UDF, Max(NFAT) AS UNF FROM TXF2"

Set db = Currentdb

Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

If Not rs.BOF Then

   A = rs!UDF

   B = rs!UNF

End If

Nota: Per l'uso di Last() vedi http://support.microsoft.com/kb/208190.

La risposta è stata utile?

0 commenti Nessun commento

2 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2014-02-10T18:59:19+00:00

    Grazie Mimmo e Karl.

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2014-02-07T13:29:36+00:00

    Ciao Luigi,

    se salvi la query, puoi utilizzare la funzione DLookUp per estrarre i due dati ad es.:

    A=dLookUp("UDF","nomeQuery")

    B=dLookUp("UNF","nomeQuery")

    altrimenti su codice devi trattare la query come recordset e quindi trattare le colonne restituite.

    Dim myrst as recordset

    set myrst=CurrentDb.Openrecordset("SELECT Last(TXF2.DFAT) AS UDF, Max(TXF2.NFAT) AS UNF FROM TXF2"

    A=myrst("UDF")

    B=myrst("UNF")

    set myrst=nothing

    Mimmo

    La risposta è stata utile?

    0 commenti Nessun commento