Exit Sub if Value is Null

JithuAccess

Member
Local time
Today, 11:21
Joined
Mar 3, 2020
Messages
325
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
 
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
 
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
 
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

Back
Top Bottom