PDA

View Full Version : [SOLVED:] Trying to allow a TextBox entry on UserForm store in variable



HTSCF Fareha
04-25-2021, 06:57 AM
I think that this is what the technical phrase is for what I'm trying to achieve?

I have the following set up in a Module on my UserForm. What I'm trying to achive is that the entry made on the Userform for RMSNum, is stored in a variable and then used to populate a message string when the routine is executed. At the moment I have nothing at all, or the default number of '44210' which is populated in the UserForm as a default value.

I'm sure that I'm making a silly basic error, but trawling the InterWeb is not helping me at all. The code below is the main parts used in the module.

Option Explicit

Sub btnSend_Click()
Dim oFrm As frmSend
Dim ws As Worksheet
Dim i As Integer
Dim txtRMSNum As String
Dim sMessage As String


Set oFrm = New frmSend
With oFrm

' Get RMS number

txtRMSNum = oFrm.txtRMSNum.Value


' Message
sMessage = "Dear Colleague," & vbCr & vbCr & _
"Please find attached a report of " & txtRMSNum & " incident that occurred. This incident is transferred to you for recording purposes." & vbCr & vbCr & _
"We will ensure all necessary steps are taken in relation to a thorough inspection taking place." & vbCr & vbCr & _
"We await your reference by return" & vbCr & vbCr & _
"Kind Regards," & vbCr & _
"Triage Hub"

End With

lbl_Exit:
Unload oFrm
Set oFrm = Nothing
Set oRng = Nothing
Exit Sub
End Sub


Many thanks!

Paul_Hossler
04-25-2021, 08:28 AM
It's a little hard to follow with just the code shared, but maybe this attachment will help

If you really want it stored in a variable then

1. Define a Public variable in a standard module

2. In the appropriate UF event, put the textbox value in the Public variable

3. Close the UF


Otherwise, attach a small workbook with the UF and other macros

HTSCF Fareha
04-25-2021, 10:40 AM
Thanks, Paul! I'll have to go with option two please.

Paul_Hossler
04-25-2021, 11:16 AM
OK

Look at the lines I changed marked with !!!! to see if they do what you want

HTSCF Fareha
04-25-2021, 12:03 PM
Thank you, Paul. I don't think that I would've come up with that!

Is there a simple explanation of what a public variable does over a normal one?

Looking at my form, I'm guessing that the ComboBox part will require the same treatment.

Thanks again!
Steve

Paul_Hossler
04-25-2021, 12:32 PM
https://docs.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-scope-and-visibility




Scope refers to the availability of a variable (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#variable), constant (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#constant), or procedure (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#procedure) for use by another procedure.

There are three scoping levels: procedure-level (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#procedure-level), private module-level (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#module-level), and public module-level.

You determine the scope of a variable when you declare it.

It's a good idea to declare all variables explicitly to avoid naming-conflict errors between variables with different scopes.


and


If you declare a module-level variable as public, it's available to all procedures in the project.

Edit: You shouldn't go too crazy with Public variables, but in this case I felt it was the most straight forward

If possible pass parameters in the call to functions or subs

HTSCF Fareha
04-26-2021, 01:26 AM
Many thanks, Paul for pointing out the advice concerning variables.

I suppose I'm still curious as to why you shouldn't use Public variables all the time, after all, this would remove any issue like I was experiencing and make things simpler so that a reference can be made to utilize that variable within a UserForm, Procedure, Module or Class Module.

snb
04-26-2021, 04:31 AM
Seems to be sufficient:


Sub btnSend_Click()
msgbox replace("Dear Colleague,~~Please find attached a report of " & txtRMSNum & " incident that occurred. This incident is transferred to you for recording purposes.~~We will ensure all necessary steps are taken in relation to a thorough inspection taking place.~~We await your reference by return.~~Kind Regards,~~Triage Hub","~",vblf)
End Sub

Paul_Hossler
04-26-2021, 06:08 AM
Many thanks, Paul for pointing out the advice concerning variables.

I suppose I'm still curious as to why you shouldn't use Public variables all the time, after all, this would remove any issue like I was experiencing and make things simpler so that a reference can be made to utilize that variable within a UserForm, Procedure, Module or Class Module.

I believe the 'standard answer' is that Public variables are harder to debug since they can be changed from ANYWHERE in the project

Same for Module variables since they can be changed by ANY sub for function in that module

1 JPG = 1K TXT

Run the 'drv' subs

In a standard module



Option Explicit


Dim X As Long


Sub drv1()
Call One(10)
Call One(20)
Call One(30)
End Sub
Sub One(X As Long) ' X is scoped to just this PROCEDURE
MsgBox X
End Sub


'--------------------------------------------------------------------------------
Sub drv2()
X = 15 ' X is scoped to just this MODULE
Call Two
X = 25 ' X is scoped to just this MODULE
Call Two
X = 35 ' X is scoped to just this MODULE
Call Two


Call TwoA
MsgBox X ' where did 9876 come from??
End Sub






Sub Two()
MsgBox X ' X is scoped to just this MODULE
End Sub
Sub TwoA()
'lots of code
X = 1234
TwoB
End Sub
Sub TwoB()
'even more lots of code
X = 5678
Call TwoC
End Sub
Sub TwoC()
'even more lots of code
X = 9876
End Sub




'--------------------------------------------------------------------------------
Sub drv3()
Y = 50 ' Y is PUBLIC (Module2)
Call Three
Y = 60 ' Y is PUBLIC (Module2)
Call Three
Y = 70 ' Y is PUBLIC (Module2)
Call Three

Call ThreeA
MsgBox Y ' where did the 4000 come from
End Sub


Sub Three()
MsgBox Y ' Y is PUBLIC (Module2)
End Sub
Sub ThreeB()
'even more lots of code
Y = 3000
Call ThreeC
End Sub





Another standard module



Option Explicit


Public Y As Long


Sub drv4()
MsgBox X
End Sub


Sub drv5()
MsgBox Y
End Sub


'sub and functions can be PUBLIC also
Public Sub ThreeA()
'lots of code
Y = 2000
ThreeB
End Sub
Public Sub ThreeC()
'even more lots of code
Y = 4000
End Sub

SamT
04-26-2021, 07:51 AM
In a Module:


Dim = Private
Private = only available to this module
Public = Available to all in this Project and other Projects in all Applications with a reference to this Project

There are a bunch of other rules applying to Scope Declarations but, IMO, those are the most important

For example:
In a Workbook Named "AcctsRCVBL" you have Public Variables Named


YTD
Qtr1
Qtr2
Etc


In a different Workbook or Appplication, you "set a Reference" to AcctsRCVBL, you can use
X = AcctsRCVBL.Qtr1
Unfortunately, you can also do
AcctsRCVBL.Qtr1 = 42
There are methods, (Properties,) so that other Projects cannot Write to those "Variables."

For Procedure Declarations, No Prefix = Private: Sub Test() is the same as Private Sub Test()

HTSCF Fareha
04-27-2021, 04:23 AM
Paul and SamT, sorry for the slow reply! Windows 10 decided to do a rather tedious update that took a few hours to complete.


Thank you to both for these explanations and code examples. It's a great shame that informative and useful items like this cannot have their own area on the forum as I am sure that many would benefit. I have created my own folder of useful VBA stuff which I add to as I go along. Whilst I humbly acknowledge the expertise of Forum members such as yourself, I feel that I have a long way to go to become reasonably proficient. Still, stick to small steps and try to learn as you go along.

Paul, I have managed to follow the examples that you have provided and I think I've managed to establish how variables can be read across different subs, with some being redefined or "ignored".

Thanks!
Steve