Consulting

Results 1 to 4 of 4

Thread: Message Box Display of Variable From a Different Sub

  1. #1
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    4
    Location

    Question Message Box Display of Variable From a Different Sub

    I am working on expanding a project where someone has the ability to select something from a Combo Box which will then call a sub to insert various data into the document. Some of the information in the sub I would like to be able to display allowing the user to click on a Button.

    Private Sub BtnClick()
    Dim TextFromButtonClick as String
     If Combobox.Value = "Data 1" Then
     '' I would like to set TextFromButtonClick to equal TextToDisplay from Data1
    Else If Combobox.Value = "Data 2" Then
     '' I would like to set TextFromButtonClick to equal TextToDisplay from Data2
    End If
     msgbox TextFromButtonClick 
    End Sub
    
    Private Sub Data1()
     Dim TextToDisplay as String
     ' Other Various Information to insert
     TextToDisplay = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    End Sub
    
    Private Sub Data2()
     Dim TextToDisplay as String
     ' Other Various Information to insert
     TextToDisplay = "Curabitur eu est libero."
    End Sub
    Any help would be appreciated.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    I don't really understand what you are trying to do or the purpose of Sub Data1() or Sub Data(2), but you could use a module level variable:

    Option Explicit
    Private m_TextToDisplay As String
    Private Sub BtnClick()
      If ComboBox.Value = "Data 1" Then
        Data1
      ElseIf ComboBox.Value = "Data 2" Then
        Data2
      End If
    End Sub
    Private Sub Data1()
      m_TextToDisplay = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    End Sub
    Private Sub Data2()
      m_TextToDisplay = "Curabitur eu est libero."
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    4
    Location
    Greg:

    This file is used by officers to select different charges that are being filed against a defendant. Each Sub Data1(), Sub Data2() are different sub routines that insert the data contained within them (i.e. statute description, title, section, offense grading, etc.) into the word document.

    What I am trying to accomplish is to allow the officer the ability to view the statute description without having to view it from a third party source (i.e. the legislation website).

    Utilizing the code you provided above, I am able to get this to work. The only thing I would like to make it do differently is have it not run the sub routine, but just pull the statute description defined in each sub routine and display it in the message box.

    I hope this makes a little more sense now.

    Thank you for any help you might be able to provide.

    Brad

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    You could put the status description in another column of the combobox:

    Private Sub CommandButton1_Click()
      MsgBox "You picked " & ComboBox1.Column(0) & " and the text associated with this item is " & ComboBox1.Column(1)
    End Sub
    
    Private Sub UserForm_Initialize()
    Dim lngIndex As Long
    Dim A() As String, B() As String
      A = Split("Data1,Data2,Data3,Data4,Data5,Data6", ",")
      B = Split("AAA,BBB,CCC,DDD,EEE,FFF", ",")
      For lngIndex = 0 To UBound(A)
        With ComboBox1
          .AddItem
          .List(.ListCount - 1, 0) = A(lngIndex)
          .List(.ListCount - 1, 1) = B(lngIndex)
        End With
      Next
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •