referencing items in a list box

R

Ryan

This is a 2 part question.

1. I was wondering how to check to see if an item is in
a listbox through code. For example: I want to check if
item1 is located in listbox1.
-The reason why is I want to perform different actions
depending on which values are contained in the list box
(it gets its values through a query)

2. How would I go about referencing a selected item in a
list box, I don't know the syntax for something like:
If item1 is selected in listbox1 then perform action1.

Any help would be greatly appreciated.

Ryan

PS sorry if it is confusing the way I worded the
question. Please let me know if you need more
clarification.
 
J

John Vinson

This is a 2 part question.

1. I was wondering how to check to see if an item is in
a listbox through code. For example: I want to check if
item1 is located in listbox1.
-The reason why is I want to perform different actions
depending on which values are contained in the list box
(it gets its values through a query)

You could do this by looping through the Listbox's records, but it
might be simpler just to use DLookUp directly on the Query:

If IsNull(DLookUp(strSoughtValue, "[listboxquery]") Then
<it's not there>
Else
2. How would I go about referencing a selected item in a
list box, I don't know the syntax for something like:
If item1 is selected in listbox1 then perform action1.

The Bound Column of the selected row is simply referenced as the value
of the listbox itself.

If Me!listbox = "SuchAndSuch" Then
 
R

Ryan

John,
Sorry for bothering you again but this is what I
entered into my code

If IsNull(DLookup("FULL
SCREENING", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If

To give you a little background, I am creating an
application for a company where customers order reports
and I am keeping a database of the reports each customer
orders (FULL SCREENING being one of them.) Then, if a
company has ordered reports, I want the appropriate tabs
on a tab control form to be enabled or not enabled in
order to output information about the report.

When I enter this code then run it I get the following
error message.

Run Time Error 3075:
Syntax error (missing operator) in query
expression 'FULL SCREENING'


Can you please tell me what you think I am doing wrong?

I am fairly new to access so sorry if these are obvious
questions.

Thanks again,
Ryan
-----Original Message-----
This is a 2 part question.

1. I was wondering how to check to see if an item is in
a listbox through code. For example: I want to check if
item1 is located in listbox1.
-The reason why is I want to perform different actions
depending on which values are contained in the list box
(it gets its values through a query)

You could do this by looping through the Listbox's records, but it
might be simpler just to use DLookUp directly on the Query:

If IsNull(DLookUp(strSoughtValue, "[listboxquery]") Then
<it's not there>
Else
2. How would I go about referencing a selected item in a
list box, I don't know the syntax for something like:
If item1 is selected in listbox1 then perform action1.

The Bound Column of the selected row is simply referenced as the value
of the listbox itself.

If Me!listbox = "SuchAndSuch" Then



.
 
D

Douglas J. Steele

Anytime you have embedded blanks, you need to enclose the name in []:


If IsNull(DLookup("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If


--
Doug Steele, Microsoft Access MVP



Ryan said:
John,
Sorry for bothering you again but this is what I
entered into my code

If IsNull(DLookup("FULL
SCREENING", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If

To give you a little background, I am creating an
application for a company where customers order reports
and I am keeping a database of the reports each customer
orders (FULL SCREENING being one of them.) Then, if a
company has ordered reports, I want the appropriate tabs
on a tab control form to be enabled or not enabled in
order to output information about the report.

When I enter this code then run it I get the following
error message.

Run Time Error 3075:
Syntax error (missing operator) in query
expression 'FULL SCREENING'


Can you please tell me what you think I am doing wrong?

I am fairly new to access so sorry if these are obvious
questions.

Thanks again,
Ryan
-----Original Message-----
This is a 2 part question.

1. I was wondering how to check to see if an item is in
a listbox through code. For example: I want to check if
item1 is located in listbox1.
-The reason why is I want to perform different actions
depending on which values are contained in the list box
(it gets its values through a query)

You could do this by looping through the Listbox's records, but it
might be simpler just to use DLookUp directly on the Query:

If IsNull(DLookUp(strSoughtValue, "[listboxquery]") Then
<it's not there>
Else
2. How would I go about referencing a selected item in a
list box, I don't know the syntax for something like:
If item1 is selected in listbox1 then perform action1.

The Bound Column of the selected row is simply referenced as the value
of the listbox itself.

If Me!listbox = "SuchAndSuch" Then



.
 
R

Ryan

John and Douglas,
I really appreciate both of your help. I am still having
a problem with my code. When I enter:

If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If


I get Run Time Error 2001:
You cancelled the previous operation.

Can you guys tell me what you think is causing this? and
what I can do to fix it? Thanks

Ryan

PS: John I will fix the spacing in the report fields
since you said it is not a good idea. Thanks for the
advice!
-----Original Message-----
When I enter this code then run it I get the following
error message.

Run Time Error 3075:
Syntax error (missing operator) in query
expression 'FULL SCREENING'

Since you (unwisely, in my opinion) have a blank in your fieldname,
you must enclose it in square brackets: [FULL SCREENING] in the
quotes.


.
 
J

John Vinson

When I enter:

If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If


I get Run Time Error 2001:
You cancelled the previous operation.

What event are you using for this code? Could you post the entire Sub
containing it?
 
R

Ryan

Here is the entire module that the code is contained in:
I entered the DLookup right after the form was opened
that contains the list box in question. I tried to
insert the DLookup other places but it still didn't work
(Sometimes I got rid of the error but it did not perform
the function I wanted it to.)

Private Sub cmdCancel_Click()
On Error GoTo Err_cmdCancel_Click

DoCmd.Close

Exit_cmdCancel_Click:
Exit Sub

Err_cmdCancel_Click:
MsgBox Err.Description
Resume Exit_cmdCancel_Click

End Sub

Private Sub cmdSearch_Click()
Dim stDocName As String
Dim stLinkCriteria As String
Dim intIncInfo As String

If txtLName <> "" And cboCustomer <> "" Then
stDocName = "frmResults"
Cmd.OpenForm stDocName, , , stLinkCriteria
If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If
Else
intIncInfo = MsgBox("You must enter part of the " & _
"applicant's last name and the company who " & _
"ordered " & vbCrLf & "the report before " & _
"searching for a record.", 16, "Incomplete Data")
End If

End Sub


Please let me know if you can think of what the problem
may be.

Thanks again,
Ryan
-----Original Message-----
When I enter:

If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If


I get Run Time Error 2001:
You cancelled the previous operation.

What event are you using for this code? Could you post the entire Sub
containing it?


.
 
H

Hugh O'Neill

Ryan said:
Here is the entire module that the code is contained in:
I entered the DLookup right after the form was opened
that contains the list box in question. I tried to
insert the DLookup other places but it still didn't work
(Sometimes I got rid of the error but it did not perform
the function I wanted it to.)

Private Sub cmdCancel_Click()
On Error GoTo Err_cmdCancel_Click

DoCmd.Close

Exit_cmdCancel_Click:
Exit Sub

Err_cmdCancel_Click:
MsgBox Err.Description
Resume Exit_cmdCancel_Click

End Sub

Private Sub cmdSearch_Click()
Dim stDocName As String
Dim stLinkCriteria As String
Dim intIncInfo As String

If txtLName <> "" And cboCustomer <> "" Then
stDocName = "frmResults"
Cmd.OpenForm stDocName, , , stLinkCriteria
If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If
Else
intIncInfo = MsgBox("You must enter part of the " & _
"applicant's last name and the company who " & _
"ordered " & vbCrLf & "the report before " & _
"searching for a record.", 16, "Incomplete Data")
End If

End Sub


Please let me know if you can think of what the problem
may be.

Thanks again,
Ryan
-----Original Message-----
When I enter:

If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If


I get Run Time Error 2001:
You cancelled the previous operation.

What event are you using for this code? Could you post the entire Sub
containing it?


.


PMFJI, John, but as Ryan has posted his code several times, just
possibly you may not have noticed that his If IsNull . . . line is not
a proper line. It appears broken without the line break code.

Hugh
 
R

Ryan

Hugh,
Thanks for the suggestion but in my actual code, there
isn't a line break. I simply posted it that way because
I ran out of room. If you can think of what is causing
this problem please let me know, it is very frustrating.

Thanks again,
Ryan
-----Original Message-----
Ryan said:
Here is the entire module that the code is contained in:
I entered the DLookup right after the form was opened
that contains the list box in question. I tried to
insert the DLookup other places but it still didn't work
(Sometimes I got rid of the error but it did not perform
the function I wanted it to.)

Private Sub cmdCancel_Click()
On Error GoTo Err_cmdCancel_Click

DoCmd.Close

Exit_cmdCancel_Click:
Exit Sub

Err_cmdCancel_Click:
MsgBox Err.Description
Resume Exit_cmdCancel_Click

End Sub

Private Sub cmdSearch_Click()
Dim stDocName As String
Dim stLinkCriteria As String
Dim intIncInfo As String

If txtLName <> "" And cboCustomer <> "" Then
stDocName = "frmResults"
Cmd.OpenForm stDocName, , , stLinkCriteria
If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If
Else
intIncInfo = MsgBox("You must enter part of the " & _
"applicant's last name and the company who " & _
"ordered " & vbCrLf & "the report before " & _
"searching for a record.", 16, "Incomplete Data")
End If

End Sub


Please let me know if you can think of what the problem
may be.

Thanks again,
Ryan
-----Original Message-----
When I enter:

If IsNull(DLookup
("[FULL SCREENING]", "qryReport_TypeResults")) Then
Forms!frmResults!tabFullScreening.Enabled = False
End If


I get Run Time Error 2001:
You cancelled the previous operation.

What event are you using for this code? Could you
post
the entire Sub
containing it?

John W. Vinson[MVP]
Come for live chats every Tuesday and Thursday
http://go.compuserve.com/msdevapps? loc=us&access=public
.


PMFJI, John, but as Ryan has posted his code several times, just
possibly you may not have noticed that his If IsNull . . . line is not
a proper line. It appears broken without the line break code.

Hugh
.
 

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