Exit Sub if Value is Null (1 Viewer)

JithuAccess

Member
Local time
Today, 14:06
Joined
Mar 3, 2020
Messages
297
Hello Guys,

This is my Code

Code:
Private Sub strName_GotFocus()

Dim db As Database
Dim rst As Recordset
Dim STRSQL As String

Set db = CurrentDb()

STRSQL = "Select [tblMaster Table].[strID], [tblMaster Table].[strName],[tblMaster Table].[strKeywords],[tblMaster Table].[datMeeting Date],[tblMaster Table].[strStatus of Application] from [tblMaster Table] where [tblMaster Table].[strPHN]=" & "'" & Me.strPHN & "';"

Set rst = db.OpenRecordset(STRSQL)

If Me.strID = Null Then
    Exit Sub
End If

If rst.RecordCount = 0 Then
    MsgBox "This ID " & Me.strID & " Not Found in Master Table, Please check the ID", vbInformation
    Me.strID.SetFocus
    Exit Sub
End If

Me.strPHN = rst.Fields(1)
Me.strName = rst.Fields(2)
Me.strKeywords = rst.Fields(3)
Me.datMeeting_Date = rst.Fields(4)

Me.Recalc

End Sub

If a User leave the strID Field Blank, I don't want to Display any messages. But this is not working. Though I don't enter any value in the ID field on the Got Focus of strName I am getting the Message. Could you please let me know how to solve this.

Thanks
 

Minty

AWF VIP
Local time
Today, 20:06
Joined
Jul 26, 2013
Messages
10,355
You test for Null in VBA like this

If IsNull(Me.strID) Then

However, that won't catch an empty string (if you can have a string value) so an alternative is

If Len(Me.strID & "") < 1 Then
 

JithuAccess

Member
Local time
Today, 14:06
Joined
Mar 3, 2020
Messages
297
You test for Null in VBA like this

If IsNull(Me.strID) Then

However, that won't catch an empty string (if you can have a string value) so an alternative is

If Len(Me.strID & "") < 1 Then
It worked Perfect. Thanks a lot
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:06
Joined
Sep 21, 2011
Messages
14,047
Hello Guys,

This is my Code

Code:
Private Sub strName_GotFocus()

Dim db As Database
Dim rst As Recordset
Dim STRSQL As String

Set db = CurrentDb()

STRSQL = "Select [tblMaster Table].[strID], [tblMaster Table].[strName],[tblMaster Table].[strKeywords],[tblMaster Table].[datMeeting Date],[tblMaster Table].[strStatus of Application] from [tblMaster Table] where [tblMaster Table].[strPHN]=" & "'" & Me.strPHN & "';"

Set rst = db.OpenRecordset(STRSQL)

If Me.strID = Null Then
    Exit Sub
End If

If rst.RecordCount = 0 Then
    MsgBox "This ID " & Me.strID & " Not Found in Master Table, Please check the ID", vbInformation
    Me.strID.SetFocus
    Exit Sub
End If

Me.strPHN = rst.Fields(1)
Me.strName = rst.Fields(2)
Me.strKeywords = rst.Fields(3)
Me.datMeeting_Date = rst.Fields(4)

Me.Recalc

End Sub

If a User leave the strID Field Blank, I don't want to Display any messages. But this is not working. Though I don't enter any value in the ID field on the Got Focus of strName I am getting the Message. Could you please let me know how to solve this.

Thanks
Why do all that work if the strID is Null ?
Test it before you try and open a recordset.
 

Users who are viewing this thread

Top Bottom