Listbox VBA Coding

A

Asif

Hi, I have a form with a listbox below is a command button
cmdDeleteRecord which the user clicks and it will open up another form
with details of the entry in the listbox highlighted. What I want to
do is when no entry is highlighted in the listbox and the user clicks
on the command button then an error message appears saying "Please
choose and entry from the list".

I've tried the following code but unfortunately when an entry is
selected the error message still comes up and the form doesn't load.

Private Sub cmdDeleteRecord_Click()
On Error GoTo Err_cmdDeleteRecord_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "2 -c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Err_cmdDeleteRecord_Click:
MsgBox "Please Choose and Entry from Cable End Skip!", 777,
"Information Missing"
End Sub

Any Suggestions as to what I'm doing wrong??

Thanks
 
S

storrboy

If the listbox's MultiSelect property is set to none, then just check
it's value...
If Nz(Me!ListBoxName,"")="" Then MsgBox "Nothing Selected"

I can't tell fro your code what the listbox name is so, you'll have to
try and insert something like that where appropriate.

If the MultiSelect is enabled then use the ItemsSelected collection

If Me!ListBoxName.ItemsSelected.Count=0 Then.....
 
A

Asif

If the listbox's MultiSelect property is set to none, then just check
it's value...
If Nz(Me!ListBoxName,"")="" Then MsgBox "Nothing Selected"

I can't tell fro your code what the listbox name is so, you'll have to
try and insert something like that where appropriate.

If the MultiSelect is enabled then use the ItemsSelected collection

If Me!ListBoxName.ItemsSelected.Count=0 Then.....

I've applied the modification to my as per your suggestions but if
still I'm having no luck the form opens even if nothing is selected in
the listbox and no message appears. maybe my coding is not correct

Private Sub cmdDeleteRecord_Click()
Dim stDocName As String
Dim stLinkCriteria As String
If Nz(Me!List23, "") = "" Then MsgBox "Nothing Selected" Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub

Thanks
 
A

Asif

If the listbox's MultiSelect property is set to none, then just check
it's value...
If Nz(Me!ListBoxName,"")="" Then MsgBox "Nothing Selected"

I can't tell fro your code what the listbox name is so, you'll have to
try and insert something like that where appropriate.

If the MultiSelect is enabled then use the ItemsSelected collection

If Me!ListBoxName.ItemsSelected.Count=0 Then.....

I've applied the modifiactions to my coding as per your suggestion
above, but I'm not getting the results I'm after. If nothing is
selected the form opens up and no message box appears. I'm not very
good in vba so maybe i've just placed your syntax in the wrong part of
my coding

Private Sub cmdDeleteRecord_Click()
Dim stDocName As String
Dim stLinkCriteria As String
If Nz(Me!List23, "") = "" Then MsgBox "Nothing Selected" Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
End Sub

Thanks
 
R

RoyVidar

Asif said:
If the listbox's MultiSelect property is set to none, then just
check it's value...
If Nz(Me!ListBoxName,"")="" Then MsgBox "Nothing Selected"

I can't tell fro your code what the listbox name is so, you'll have
to try and insert something like that where appropriate.

If the MultiSelect is enabled then use the ItemsSelected collection

If Me!ListBoxName.ItemsSelected.Count=0 Then.....

I've applied the modification to my as per your suggestions but if
still I'm having no luck the form opens even if nothing is selected
in the listbox and no message appears. maybe my coding is not correct

Private Sub cmdDeleteRecord_Click()
Dim stDocName As String
Dim stLinkCriteria As String
If Nz(Me!List23, "") = "" Then MsgBox "Nothing Selected" Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub

Thanks

Try

Private Sub cmdDeleteRecord_Click()

Dim stDocName As String
Dim stLinkCriteria As String

If Nz(Me!List23, "") = "" Then
MsgBox "Nothing Selected"
Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
End Sub

Here's another test for no selection

if Me!List23.ListIndex = -1 then
' no selection
end if
 
G

Guest

Asif,

I take a different approach to handling this issue.

When I have a list box where the user must make a selection before an action
is appropriate, I will set the enabled property of the command button to
false. Then in the AfterUpdate event of the listbox I would use code to
enable the command button only if there is a selection made in the list box.

As storrboy as stated, the code would be different based on the status of
the multi-select property setting of the listbox.

If the multi-select property of the listbox is set to "None" then the code
would be:

If not isnull(Me!List23) then
Me.cmdDeleteRecord.enabled = true
End If

If the multi-select property of the listbox is set to anything else, you
would check the ItemsSelected count value.

If me.list23.ItemsSelected.count > 0 then
Me.cmdDeleteRecord.enabled = true
Else
Me.cmdDeleteRecord.enabled = false
End If

Using this method, you are being proactive and not reactive, meaning that
you never have to try to interrupt the action of the command button. It will
simply not be available unless an item is seleted from your list box.

--
HTH

Mr B
email if needed to:
draccess at askdoctoraccess dot com


Asif said:
If the listbox's MultiSelect property is set to none, then just check
it's value...
If Nz(Me!ListBoxName,"")="" Then MsgBox "Nothing Selected"

I can't tell fro your code what the listbox name is so, you'll have to
try and insert something like that where appropriate.

If the MultiSelect is enabled then use the ItemsSelected collection

If Me!ListBoxName.ItemsSelected.Count=0 Then.....

I've applied the modifiactions to my coding as per your suggestion
above, but I'm not getting the results I'm after. If nothing is
selected the form opens up and no message box appears. I'm not very
good in vba so maybe i've just placed your syntax in the wrong part of
my coding

Private Sub cmdDeleteRecord_Click()
Dim stDocName As String
Dim stLinkCriteria As String
If Nz(Me!List23, "") = "" Then MsgBox "Nothing Selected" Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
End Sub

Thanks
 
A

Asif

<[email protected]>:




I've applied the modification to my as per your suggestions but if
still I'm having no luck the form opens even if nothing is selected
in the listbox and no message appears. maybe my coding is not correct
Private Sub cmdDeleteRecord_Click()
Dim stDocName As String
Dim stLinkCriteria As String
If Nz(Me!List23, "") = "" Then MsgBox "Nothing Selected" Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub

Try

Private Sub cmdDeleteRecord_Click()

Dim stDocName As String
Dim stLinkCriteria As String

If Nz(Me!List23, "") = "" Then
MsgBox "Nothing Selected"
Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
End Sub

Here's another test for no selection

if Me!List23.ListIndex = -1 then
' no selection
end if

Thanks Roy its working exactly as I wanted it too. Out of curiosity
what is "Nz" in the If statement?
 
R

RoyVidar

Asif said:
<[email protected]>:




If the listbox's MultiSelect property is set to none, then just
check it's value...
If Nz(Me!ListBoxName,"")="" Then MsgBox "Nothing Selected"
I can't tell fro your code what the listbox name is so, you'll
have to try and insert something like that where appropriate.
If the MultiSelect is enabled then use the ItemsSelected
collection If Me!ListBoxName.ItemsSelected.Count=0 Then.....
I've applied the modification to my as per your suggestions but if
still I'm having no luck the form opens even if nothing is selected
in the listbox and no message appears. maybe my coding is not
correct Private Sub cmdDeleteRecord_Click()
Dim stDocName As String
Dim stLinkCriteria As String
If Nz(Me!List23, "") = "" Then MsgBox "Nothing Selected" Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub

Try

Private Sub cmdDeleteRecord_Click()

Dim stDocName As String
Dim stLinkCriteria As String

If Nz(Me!List23, "") = "" Then
MsgBox "Nothing Selected"
Else
stDocName = "2-c CANCELJOB"
stLinkCriteria = "[BINPROCESSID]=" & Me![txtBinProcessID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
End Sub

Here's another test for no selection

if Me!List23.ListIndex = -1 then
' no selection
end if

Thanks Roy its working exactly as I wanted it too. Out of curiosity
what is "Nz" in the If statement?

A little task for you - in the VB editor, set the cursor within the NZ
function name, and hit F1 - you should get the help file topic on the
function;-)
 

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