Possible Duplicate:
Code for Connecting to Folder/File to open a mxd document!

I tried to make the title as thorough as I could. I need some help with VBA code for my dialog box. I have written code to add items into the combo box when the form is initiated but I am having trouble understanding how to pass the user selection into the code of my create map command button after it is clicked. My code is displayed below:

Private Sub UserForm_Initialize()

'Add Items to the Drop down List

cboStations.AddItem ("Annual")
cboStations.AddItem ("Urban")

cboDistrict.AddItem ("Abilene")
cboDistrict.AddItem ("Amarillo")
cboDistrict.AddItem ("Atlanta")
cboDistrict.AddItem ("Austin")
cboDistrict.AddItem ("Beaumont")
cboDistrict.AddItem ("Brownwood")

End Sub

Private Sub cmdMap_Click()

    Dim userSelect As String
    userSelect = ComboBox(" ")
    Call CreateMap(userSelect)

End Sub

Private Sub CreateMap()

    Dim cboStations As String
    Dim cboDistrict As String

    Dim userValue As String
    userValue = cboDistrict.Text

    Dim mapDocument As IMapDocument
    Set mapDocument = New mapDocument

    If mapDocument.IsPresent("K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\District_Maps\2012\Abilene\Abilene_Base_Map.mxd") Then
    mapDocument.Open ("K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\District_Maps\2012\Abilene\Abilene_Base_Map.mxd")
        MsgBox "District Does Not Exist"
    End If

1 Answer 1


At some point you will need to test the value of userValue and use that to build the path to the map document you are trying to open. I am not well versed in vba, so unfortunately I can't give a highly detailed explanation of how to accomplish this. I can offer some direction:

This will likely require multiple If/Else statements, probably one for each county you want to have a unique map for.

I would declare a string that is the path and then set that value for each condition in the result of your If/Else statements that evaluate userValue. So, if the user has picked "Abilene" the code sets the path string equal to the path of that map. If the user picks "El Paso" then the code sets the path string equal to the path of that map, and so on and so forth.

If userValue = 'Abilene' Then
    path = "[Drive]\Folder\Folder\Abilene.mxd"
ElIf userValue = 'El Pase' Then
    path = "[Drive]\Folder\Folder\ElPaso.mxd"
*{Series of ElIfs that deal with your other Counties Goes Here}*

Then, once the path string is built you use the If statement you have, but instead of passing the function the whole string, you just pass it the one you built in the If/Else statements above:

If mapDocument.IsPresent(path) Then
    mapDocument.Open (path)
    MsgBox "District Does Not Exist"
End If

As I said, I do now know if that is the proper syntax for those functions, I'm just hoping that this can give you some direction.


Not the answer you're looking for? Browse other questions tagged or ask your own question.