Full Version: Cascading Combo Boxes?
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
niallmacmillan
Hi All,


I am new to this Cascading Combo Boxes stuff and my code will not work.

Am I being really Dumb.

My first Cbo Box is NAME and my second is NUMBER
The table I am pulling this from is called VAN ACCIDENTS-ISSUES
My fields are DRIVER and ACCIDENTNO.

Please help, I need this ready by 11am UK time!!

CODE
Private Sub Name_AfterUpdate()

On Error Resume Next

Number.RowSource = "Select distinct AccidentNo " & _
"FROM Van Accidents-Issues " & _
"WHERE Driver = '" & Name & "' " & _
"ORDER BY AccidentNo "

End Sub


Thanks Again!!!
GroverParkGeorge
Welcome to UtterAccess. For starters, both Name and Number are reserved words in Access and SHOULD NOT be used for field names. Change them to something less generic and more relevant to the content.

Oh, and what does it mean to say, the "code will not work". Does it raise an error? Does it return too many rows? The wrong rows? No rows? or something else?

THanks for giving us enough information to try to suggest some possible resolutions.
niallmacmillan
Thanks for the advice. I have changed the cbo boxes to cboname and cbonumber.

The still does no populate the 2nd combo with the AccidentNO. The drop down is still showing blank. I have had many errors from trial and error and incorrect syntax and please check the FROM function.

Please see below updated code. For this one I receive an error 'Syntax error in FROM Clause'

CODE
Private Sub cboName_AfterUpdate()

On Error Resume Next

cboNumber.RowSource = "Select distinct AccidentNo " & _
"FROM Van Accidents-Issues " & _
"WHERE Driver = '" & cboName & "' " & _

"ORDER BY AccidentNo "

End Sub

Alan_G
Hi

First of all I'd suggest getting rid of On Error Resume Next and replacing it with an actual error handler. On Error Resume Next has it's place in your armoury, but this occasion definitely isn't one of them.

Second thing is you're using spaces in your object name (and a hyphen as well), which along with George's advice about not using reserved words is never a good idea. If you use spaces, you need to wrap the object name in square brackets, but ideally you should get rid of the space and hyphen in the table name and rename it to something like VanAccidentIssues.

Only other thing that I could imagine causing an error is that you're using a text datatype for the Where clause. That may well be correct as the error would then be data type mismatch if it was supposed to be numeric, but may be worth looking at if you still get errors. Also, make sure that your cboName combo is actually bound to the column that will return the correct data

Oh, and it would be a good idea to use the Me keyword as well

All that said, try this

CODE
Private Sub cboName_AfterUpdate()

Me.cboNumber.RowSource = "Select distinct AccidentNo " & _
"FROM [Van Accidents-Issues] " & _
"WHERE Driver = '" & Me.cboName & "' " & _
"ORDER BY AccidentNo "

End Sub
GroverParkGeorge
Ouch, I missed that name, Alan. Thanks for spotting it.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.