A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Code to Determine if records exist in an Unbound List Box



 
 
Thread Tools Display Modes
  #1  
Old December 16th, 2009, 06:31 PM posted to microsoft.public.access.forms
chris
external usenet poster
 
Posts: 2,039
Default Code to Determine if records exist in an Unbound List Box

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

  #2  
Old December 16th, 2009, 06:47 PM posted to microsoft.public.access.forms
Dirk Goldgar
external usenet poster
 
Posts: 2,529
Default Code to Determine if records exist in an Unbound List Box

"Chris" wrote in message
...
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.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

  #3  
Old December 16th, 2009, 07:25 PM posted to microsoft.public.access.forms
chris
external usenet poster
 
Posts: 2,039
Default Code to Determine if records exist in an Unbound List Box

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.
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 04:08 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.