Sandra Smith
asked on
Assign result of ACCESS 2003 VBA SQL to variable for use in another text box.
I have the below code in a form module attached to the Afterupdate event. However, what I need is for the result of the strTEMPSAP query to be assigned to a variable so I can use it in Me.txtTempNumber. So, how do I get the result into my variable? This is in an ACCESS 2003 mdb database.
Private Sub cboCountry_AfterUpdate()
'Reviews the tblOpenDates table, based on country, for the maximum
'number in the TempNumber field and assigns the next digit
'to create the next TempNumber for new stores
Dim strTempSAP As String
Dim strCountryCode As String
Dim numNext As Single
Dim numYearOpened As Double
numYearOpened = Year(Me.txtDate)
If Me.cboCountry = "US" Then
strCountryCode = "US"
ElseIf Me.cboCountry = "CANADA" Then
strCountryCode = "CAN"
ElseIf Me.cboCountry = "MEXICO" Then
strCountryCode = "MEX"
End If
'Gets the max TempNumber digits based on the country as inputted in the
'cboCountry combo box and the year opened in the txtYearOpened textbox.
'Tempnumbers are in the format of US2008-01, etc.
strTempSAP = "SELECT FORMAT(Right([MaxOfTempNum ber],2)+1, '00') AS NextNo " & _
"FROM [SELECT tblOpenDates.Country, Max(tblOpenDates.TempNumbe r) AS MaxOfTempNumber " & _
"FROM tblOpenDates GROUP BY tblOpenDates.Country " & _
"HAVING tblOpenDates.Country = " & Me.cboCountry & " AND " & _
"Not IsNull(Max(tblOpenDates.Te mpNumber)) ]. AS [%$##@_Alias] "
Me.txtTempNumber = strCountryCode & numYearOpened & "-" & numNext
End Sub
Private Sub cboCountry_AfterUpdate()
'Reviews the tblOpenDates table, based on country, for the maximum
'number in the TempNumber field and assigns the next digit
'to create the next TempNumber for new stores
Dim strTempSAP As String
Dim strCountryCode As String
Dim numNext As Single
Dim numYearOpened As Double
numYearOpened = Year(Me.txtDate)
If Me.cboCountry = "US" Then
strCountryCode = "US"
ElseIf Me.cboCountry = "CANADA" Then
strCountryCode = "CAN"
ElseIf Me.cboCountry = "MEXICO" Then
strCountryCode = "MEX"
End If
'Gets the max TempNumber digits based on the country as inputted in the
'cboCountry combo box and the year opened in the txtYearOpened textbox.
'Tempnumbers are in the format of US2008-01, etc.
strTempSAP = "SELECT FORMAT(Right([MaxOfTempNum
"FROM [SELECT tblOpenDates.Country, Max(tblOpenDates.TempNumbe
"FROM tblOpenDates GROUP BY tblOpenDates.Country " & _
"HAVING tblOpenDates.Country = " & Me.cboCountry & " AND " & _
"Not IsNull(Max(tblOpenDates.Te
Me.txtTempNumber = strCountryCode & numYearOpened & "-" & numNext
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it was either that or dlookup...since you already had the SQL...just seemed easier. I'm bing lazy today.
you might want to check for null or eof with the recordset...so you can defallt to 0 or all or something if that SQL doesn't return anything.
J
J
ASKER