Link to home
Start Free TrialLog in
Avatar of Karen Schaefer
Karen SchaeferFlag 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

Avatar of Dale Fye
Dale Fye
Flag of United States of America image

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.
Avatar of 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?
Select Case MsgBox("Records Found, & "'"  & strLocation & "'" &, vbYesNo Or vbExclamation Or vbDefaultButton1, ")
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
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
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I know being lazy.
no tried you suggestion however, still getting same error does it matter that it is an unbound form?
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
Thanks for the input, however, this project was cancelled I no longer need assistance with this issue.