PDA

View Full Version : Message Box Display of Variable From a Different Sub



brad71
02-02-2019, 12:36 PM
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.

gmaxey
02-02-2019, 09:02 PM
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

brad71
02-04-2019, 12:58 PM
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

gmaxey
02-04-2019, 01:13 PM
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