vba reference to combobox column value

fluffyozzy

Registered User.
Local time
Today, 00:28
Joined
May 29, 2004
Messages
63
Hi guys and girls,

What is the correct way to refer to a combobox column in vba? I'm trying to reference a combobox column on a form as a parameter in dcount statement but it's not working. It's probably something really simple but totally stumped me.

This works:
CountOfID = DCount("[PersonID]", "tblResponse", "[PersonID] = Forms!frmMain!frmMainHold!frmPerson!PersonID AND [QstID] = 1")

But this doesn't and this is the one I need:

cmbValue = Forms!frmMain!frmMainHold!frmPerson!cmbOpenDataForm!Column(0)

CountOfID = DCount("[PersonID]", "tblResponse", "[PersonID] = Forms!frmMain!frmMainHold!frmPerson!PersonID AND Forms!frmMain!frmMainHold!frmPerson!cmbOpenDataForm = cmbValue")

Can you help please? Many thanks in advance
 
if the field, PersonID is numeric:
Code:
cmbValue = Forms!frmMain!frmMainHold!frmPerson.Form!cmbOpenDataForm.Column(0)
CountOfID = DCount("[PersonID]", "tblResponse", "[PersonID] = " & cmbValue)
 
Thank you to both of you! isladogs, I should have had that link ages ago! Very useful. And arnelgp, thank you so very much, it all works perfectly. I wasn't thinking clearly at all this morning LOL. So, the final working solution is:

cmbValue = Forms!frmMain!frmMainHold!frmPerson.Form!cmbOpenDataForm.Column(0)

CountOfID = DCount("[PersonID]", "tblResponse", "[PersonID] = Forms!frmMain!frmMainHold!frmPerson!PersonID" & QstID = cmbValue)

Cheers guys x
 
Glad you've got it working but I'm surprised that works.
The syntax for the PersonID part needs to be modified in the same way to include .Form.
Also you need to allow for number or text datatype as arnelgp explained

Assuming both fields are number datatype and combining into one line then I believe you need:
Code:
CountOfID = DCount("PersonID", "tblResponse", "PersonID = " & Forms!frmMain!frmMainHold!frmPerson.Form.PersonID & " & QstID = " & Forms!frmMain!frmMainHold!frmPerson.Form.cmbOpenDataForm)
 
Last edited:
I'm surprised that works. The syntax for the PersonID part needs to be modified in the same way to include .Form.

The Form Property of the subformcontrol can be left out if you use the bang(!) operator because it is a reference to the default member.

However it is better to include the .Form. because it is Early Bound and any spelling mistakes in the control name will be picked up during compile. The bang is Late Bound so is only tested at run time.
 
Mmm.. Yes, both fields are number datatype. Just ran about 10 tests with what I have and it is doing what I wanted. To clarify, the person ID is not a combo, only the QstID is selected from a combo, PersonID is just an ordinary number field on the form :)
 
Galaxiom, good to know the proper convention, thank you. I've just tried it with my code with the way isladogs suggested and it works too. I learn more on forums than any course I paid through the nose for :D
 
The Form Property of the subformcontrol can be left out if you use the bang(!) operator because it is a reference to the default member.

Thanks Greg. I've learnt something new.
 

Users who are viewing this thread

Back
Top Bottom