1
votes

I got en error while opening my form in access.
This code should be executed with the "OnOpen event" for that form. But im getting an error in my script.

Dim ThisDB As DAO.Database
    Set ThisDB = CurrentDb
    Dim d As DAO.Recordset
    Dim q As String
    q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & sid2 'sql query
    Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)
    Dim Result As String
    Result = ""
    If d.EOF = False Or d.BOF = False Then 'if-else clause
        d.MoveFirst
        Do While Not d.EOF
            If Result <> "" Then Result = Result & "; "
            Result = Result & d!EMail
            d.MoveNext
          Loop
      End If
    d.Close

The faulty line is:

Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)
1
do a debug.print q before the openrecordset. My guess is that your sid2 is NULLThomas G
i did MsgBox qand the sid2 is not NULL. the output of MsgBox qis SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = AKPrel0aded0ne

1 Answers

1
votes

I solved the problem.

The Line

q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & sid2

was not correct.

q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & "'" & sid2 & "'"

The difference is: = " & "'" & sid2 & "'"