Avatar of Karen Schaefer
Karen Schaefer
Flag for United States of America asked on

using variable or a variable string in a Msgbox

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

Microsoft AccessVBA

Avatar of undefined
Last Comment
Karen Schaefer

8/22/2022 - Mon
Dale Fye

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 Schaefer

ASKER
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 Schaefer

ASKER
Select Case MsgBox("Records Found, & "'"  & strLocation & "'" &, vbYesNo Or vbExclamation Or vbDefaultButton1, ")
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Dale Fye

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 Schaefer

ASKER
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
ASKER CERTIFIED SOLUTION
Dale Fye

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Karen Schaefer

ASKER
I know being lazy.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Karen Schaefer

ASKER
no tried you suggestion however, still getting same error does it matter that it is an unbound form?
Dale Fye

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 Schaefer

ASKER
Thanks for the input, however, this project was cancelled I no longer need assistance with this issue.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck