How to gather the values of combo box selected item

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have four combo boxes in an access form. Users will select one item from
each of the four combo boxes. Based on the choices the selection criteria
will be used for further processing. However, I need to get the user selected
values from each of the combo box and stuff them to variables. I have the
following code just to do that. However, it is not working. Any help is
appreciated. Thanks
CODE:
Option Compare Database

Private Sub cboBusinessUnit_AfterUpdate()
strMarketChannel = Me.cboBusinessUnit.Text
End Sub

Private Sub cboGLPeriod_AfterUpdate()
strGLPeriod = Me.cboGLPeriod.Text
End Sub

Private Sub cboTbsGroup_AfterUpdate()
strTBSGroup = Me.cboTbsGroup.Text
End Sub

Private Sub cboTBSYear_AfterUpdate()
strGLYear = Me.cboTBSYear.Text
End Sub

Private Sub cmdSearchNow_Click()
MsgBox ("Procedure complete. Records selected" & vbCrLf & _
"Parameter 1: " & strMarketChannel & vbCrLf & "Parameter 2: " &
strTBSGroup & vbCrLf & "Parameter 3: " & strGLPeriod & vbCrLf & "Parameter
4: " & strGLYear)

'DoCmd.OpenForm "frmResults", acFormDS
End Sub
 
You could try placing the variables on the on click event.

Private Sub cmdSearchNow_Click()
dim strMarketChannel as string
dim strGLPeriod as string
dim strTBSGroup as string
dim strGLYear as string

strMarketChannel = Me.cboBusinessUnit.Text
strGLPeriod = Me.cboGLPeriod.Text
strTBSGroup = Me.cboTbsGroup.Text
strGLYear = Me.cboTBSYear.Text

MsgBox ("Procedure complete. Records selected" & vbCrLf & _
"Parameter 1: " & strMarketChannel & vbCrLf & "Parameter 2: " &
strTBSGroup & vbCrLf & "Parameter 3: " & strGLPeriod & vbCrLf & "Parameter
4: " & strGLYear)

'DoCmd.OpenForm "frmResults", acFormDS
End Sub
 
The Text property is only available when a control has the focus. It is more
a VB property than a VBA property. Additionally, assigning values to the
variables is an extra unneeded step. You could present your message box
using the value in the combo:

MsgBox ("Procedure complete. Records selected" & vbCrLf & _
"Parameter 1: " & Me.cboBusinessUnit & vbCrLf & "Parameter 2: " &
Me.cboTbsGroup & vbCrLf & "Parameter 3: " & strGLPeriod & vbCrLf &
"Parameter
4: " & Me.cboGLPeriod)
 
Thanks a lot Klatuu for your advise. Now I can use the value out of combo box
to use for processing. Regards.
 
Falty,
Please check Klatuus advise. It was very helpful. The text property does not
work in VBA which is my case. Hope I answered you. Thanks.
 
Back
Top