using variable or a variable string in a Msgbox

Karen Schaefer
Karen Schaefer used Ask the Experts™
on
Need help with syntax.

I would like to display the cityname, state (if applicable), & country name within the message in the message box.  these values come from the form CountryRecid, combo. column(1) and StateRecid.column(1) and the city name which is txtbox.

Private Sub cmdAdd_Click()
'Dim gCountryID As Integer
'Dim gStateID As Integer
'Dim gCityName As String
Dim n As Integer
Dim nName As String
   On Error GoTo cmdAdd_Click_Error

gCountryID = Nz([Forms]![frmAddLocation]![CountryRecID])
gStateID = Nz([Forms]![frmAddLocation]![StateRecID])
gCityName = Nz([Forms]![frmAddLocation]![CityName])
    
    n = DCount("*", "tblCity", "[CityName] Like '*" & gCityName & "*' " & _
        "AND [CountryRecID] = " & gCountryID & " " & _
        "AND [StateRecID] = " & gStateID & "")

        If n = 0 Then
            DoCmd.RunSQL _
                ("INSERT INTO tblCity ( CountryRecID, StateRecID, CityName, TaxInclusive, Top5000, DateCreated, Modifiedby ) " _
                & "VALUES ( '" & gCountryID & "', '" & gStateID & "', '" & _
                gCityName & "', " & gTaxIncl & ", " & gTop & ", Date(), '" & _
                GetgUserID() & "' )")
        Else
[b]'        nName = gCityName & ", " & [Forms]![frmAddLocation]![StateRecID].Column(1) & ", " & [Forms]![frmAddLocation]![CountryRecID].Column(1)
'       Debug.Print nName
            Select Case MsgBox("Records Found & ", "  & gCityName &   , vbYesNo Or vbExclamation Or vbDefaultButton1, ")[/b]

                Case vbYes

                Case vbNo

            End Select

        End If

   On Error GoTo 0
   Exit Sub

cmdAdd_Click_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdAdd_Click of VBA Document Form_frmAddLocation"

End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
What do you want it to look like?

You describe it well, but I'm not sure whether you are trying to put this in the messagebox in the error handler, or somewhere else within your code.  The way to create a string variable with that information would look something like:

strLocation = (me.txt_CityName + ", ") & (me.cbo_StateRecID.column(1) + ", ") _
                     & me.cbo_CountryRecID.Column(1)

I used the + sign to concatenate some of these strings to the associated commas so that the comma will not appear if the City or State value is NULL.
Karen SchaeferBI ANALYST

Author

Commented:
Cityname, State, Country

strLocation = (Me.CityName + ", ") & (Me.StateRecID.Column(1) + ", ") _
                     & Me.CountryRecID.Column(1)


now what is the proper syntax to get strlocation within the msgbox?
Karen SchaeferBI ANALYST

Author

Commented:
Select Case MsgBox("Records Found, & "'"  & strLocation & "'" &, vbYesNo Or vbExclamation Or vbDefaultButton1, ")
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
I would do:

strMsg = "Records found, " & strLocation
Select Case msgbox(strmsg, vbYesNo+VbExclamation + vbDefaultButton1)
   Case vbYes

   Case vbNo

   Case else
       Msgbox "Something is extremely wrong, that message " & vbcrlf _
                   & "box should only return vbYes or vbNo!"
End Select
Karen SchaeferBI ANALYST

Author

Commented:
thanks that work, do you know the best way to clear an unbound form - it is part of the results of the msgbox.

                    Me.txtCountryName.Value = ""
                    Me.CountryRecID.Value = ""
                    Me.StateRecID.Value = ""
i get error msg - You can assign a value to this object in procedure
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
What procedure are you trying to use?  I normally set those values to NULL, but in Access, you don't have to refer to the Value property, simply:

me.txtCountryName = NULL
me.CountryRecID = NULL
me.StateRecID = Null

BTW, I see you prefixed your CountryName control with "txt", why didn't you prefix the other two with "cbo"?  I generally rename all of my controls on my forms so that I know what type of control I'm refering to.  By default, Access will assign your data controls the name of the field that they are bound to, so when you refer to:

me.CountryRecID

you could be referring to a control, or actually to the field that is part of the forms recordsource, although the proper syntax for referring to the field would be:

me!CountryRecID

if you use a naming convention, you will always know whether your code refers to a control or a field.
Karen SchaeferBI ANALYST

Author

Commented:
I know being lazy.
Karen SchaeferBI ANALYST

Author

Commented:
no tried you suggestion however, still getting same error does it matter that it is an unbound form?
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
Shouldn't matter that it is unbound.

What event are you using for to reset these values?

I rarely use unbound forms, have you tried:

me.undo
Karen SchaeferBI ANALYST

Author

Commented:
Thanks for the input, however, this project was cancelled I no longer need assistance with this issue.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial