Run-time error 3021 (1 Viewer)

PatAccess

Registered User.
Local time
Yesterday, 19:12
Joined
May 24, 2017
Messages
284
Hello Guys,
The following code worked in another form and returns a string but
I am getting run-time error 3021 from my code. When I step through it sqlDateEarned shows a value but the value is not transmitting to strDateEarned so I am getting the error message. Can you please help me. (It is support to return a date).
Code:
Private Sub cmdDateEarned_Click()
    Dim sqlDateEarned As String
    Dim strDateEarned As Date
    
    sqlDateEarned = "SELECT DateEarned FROM [QryPE] WHERE DateEarned=#" & Me.DateEarned.Value & "# AND FName='" & Me.cboFName.Column(1) & "' AND [State]='" & Me.cboStates.Value & "'"
    strDateEarned = CurrentDb.OpenRecordset(sqlDateEarned).Fields(0).Value
    
    If IsNull(strDateEarned) Then
        MsgBox "There is no earned date for this license", vbCritical, "No Date Earned"
    Else
        MsgBox "This license was earned on " & strDateEarned & ".", , "Date Earned"
    End If

End Sub

Thank you,
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:12
Joined
Sep 21, 2011
Messages
14,299
No idea what error 3021 is, and I am not going to go looking to find out. :(
Is any data returned by the sql statement?
Why are you looking for DateEarned when that is one of the values you are supplying?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:12
Joined
Oct 29, 2018
Messages
21,473
Hi. You could just use DCount() for this, I think.
 

Micron

AWF VIP
Local time
Yesterday, 19:12
Joined
Oct 20, 2018
Messages
3,478
Or even DLookup, as it appears that there would only be one date value or no date value.

PatAccess - advise to always post the error message as well. There must be around 3,000 of them and it is just luck if anyone recognizes just what the number means. Besides, some of the numbers signify different errors, in which case it all depends on the message.
A couple of tips:
- you don't need .Value as it is the default property
- you might find using a variable prefix (e.g. str) confusing when you use it for multiple data types.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:12
Joined
Feb 19, 2013
Messages
16,612
if your query doesn't return at least one record, then the line

strDateEarned = CurrentDb.OpenRecordset(sqlDateEarned).Fields(0).Value


will error with 'can't find current record' (error 3021)

recommend use dcount as already suggested

if dcount("*","QryPE","DateEarned=#" & Me.DateEarned.Value & "# AND FName='" & Me.cboFName.Column(1) & "' AND [State]='" & Me.cboStates.Value & "'")=0 then 'no record found

else 'record found

end if

presume your country date format is US style (mm/dd/yyyy) otherwise you will need to use the format function to format it that way
 

Users who are viewing this thread

Top Bottom