Code to Determine if records exist in an Unbound List Box

C

Chris

I have and unbound form with an unbound List Box that has a query as the
record source. I use the following code to update the List Box and then
determine if there are records and take the apporpriate action.
Unfortunately, if I put the code

Forms![Post GASP Data].Controls("ListComputerID").ListIndex = 0

after the first "SetFocus" statement when no records exist, it errors out.
If I don't use this statement when records do exist, it just goes to the
first IF statement option and exits the function. How can I re-write this
code to look in the List Box for records and then take the appropriate action?

DoCmd.SetWarnings False
'This step imports new PC GASP Audit records into the
'the "GASP Audit - Server Details" table.
DoCmd.OpenQuery "Append - Computer Data"

'This call the referenced Function to update the Computer Data records
Call UpdateGASPAuditData

'This requeries the form and sets focus to the form.
Forms![Post GASP Data]![ListComputerID].Requery
Forms![Post GASP Data]![ListComputerID].SetFocus


'This checks for Nul record set and exits fucntion if true
If (IsNull(Forms![Post GASP Data]![ListComputerID])) Then
MsgBox "No New Computer Data to add to the GASP Database.",
vbInformation, "GASP Database"
Exit Function
Else
'This sets forcus to the first record
Forms![Post GASP Data].Controls("ListComputerID").ListIndex = 0
'This calls the referenced function to append the software records
Call AppendGASPComputerApplications
End If
MsgBox "The New Computer Data has been added to the GASP Database.",
vbInformation, "GASP Database"

DoCmd.SetWarnings True
 
D

Dirk Goldgar

Chris said:
I have and unbound form with an unbound List Box that has a query as the
record source. I use the following code to update the List Box and then
determine if there are records and take the apporpriate action.
Unfortunately, if I put the code

Forms![Post GASP Data].Controls("ListComputerID").ListIndex = 0

after the first "SetFocus" statement when no records exist, it errors out.
If I don't use this statement when records do exist, it just goes to the
first IF statement option and exits the function. How can I re-write this
code to look in the List Box for records and then take the appropriate
action?

DoCmd.SetWarnings False
'This step imports new PC GASP Audit records into the
'the "GASP Audit - Server Details" table.
DoCmd.OpenQuery "Append - Computer Data"

'This call the referenced Function to update the Computer Data records
Call UpdateGASPAuditData

'This requeries the form and sets focus to the form.
Forms![Post GASP Data]![ListComputerID].Requery
Forms![Post GASP Data]![ListComputerID].SetFocus


'This checks for Nul record set and exits fucntion if true
If (IsNull(Forms![Post GASP Data]![ListComputerID])) Then
MsgBox "No New Computer Data to add to the GASP Database.",
vbInformation, "GASP Database"
Exit Function
Else
'This sets forcus to the first record
Forms![Post GASP Data].Controls("ListComputerID").ListIndex = 0
'This calls the referenced function to append the software records
Call AppendGASPComputerApplications
End If
MsgBox "The New Computer Data has been added to the GASP Database.",
vbInformation, "GASP Database"

DoCmd.SetWarnings True


If I understand you correctly, change this:
If (IsNull(Forms![Post GASP Data]![ListComputerID])) Then

.... to this:

If Forms![Post GASP Data]!ListComputerID.ListCount > 0 Then

Is this code in the code module of the form [Post GASP Data] ? If so, you
could replace all occurrences of "Forms![Post GASP Data]" with "Me", the
keyword that refers to the object containing the executing code.
 
C

Chris

Thanks for the reply, I stumbled upon that after posting this question. Here
is what I added after the SetFocus statement

If Forms![Post GASP Data].Controls("ListComputerID").ListCount > 0 Then
'This sets forcus to the first record
Forms![Post GASP Data].Controls("ListComputerID").ListIndex = 0
End If

An the code is not in the form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top