How to add a textbox value to variable from a userform? - excel

I've created a userform named UIAutotestHeader and textbox named pypath. And on button click I'm trying to pass a value to a variable but getting runtime error 424. Any help please.
Sub LoopThroughFiles()
Dim Path As String
UIAutotestHeader.Show
Path = pypath.Value
If pypath.Value = "" Then
MsgBox "Please add a path having .py files."
End If
End sub
Button click code:
Private Sub CommandButton1_Click()
UIAutotestHeader.Hide
End Sub

First, see this helpful RubberDuck Blog on working with UserForms, very helpful and applicable. This is what I'm basing my answer on.
Try to instantiate your userform using a With statement so that you have a captured instance of it where you have access to its various properties that you expose.
Note, in this case, you don't have to store your variables, as you still have access to them in your instance of your userform. Here is an example below.
Sub LoopThroughFiles()
With New UIAutotestHeader
.Show
If Not .IsCancelled Then
If .PyPath = "" Then
MsgBox "Please add a path having .py files."
End If
End If
End With
End Sub
In your Userform, you can expose the properties that you want to have access to. I also added the IsCancelled method to make sure the user didn't press cancel.
Option Explicit
Private cancelled As Boolean
Public Property Get PyPath() As String
PyPath = pypath.Value
End Property
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelled
End Property
Private Sub CommandButton1_Click()
Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
OnCancel
End If
End Sub
Private Sub OnCancel()
cancelled = True
Hide
End Sub

Try this code
'In Standard Module
'------------------
Public sPath As String
Sub LoopThroughFiles()
Load UIAutotestHeader
sPath = UIAutotestHeader.pypath.Value
UIAutotestHeader.Show
End Sub
'In UserForm Module
Private Sub pypath_AfterUpdate()
If sPath = "" Then
MsgBox "Please add a path having .py files."
End If
End Sub
Private Sub CommandButton1_Click()
If sPath <> "" Then MsgBox sPath
sPath = ""
Unload UIAutotestHeader
End Sub

Related

Continue procedure if CommandButton is clicked

So far I have used the below VBA in order to continue with a procedure if the user clicked ok in the MsgBox:
Sub Button_Message_Box()
Answer = MsgBox("Do you want to continue the procedure?", vbOK)
If Answer = vbOK Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
Now I want to achieve the exact same result using CommandButton1 in UserForm1.
Therefore I tried to go with this:
(1) VBA in UserForm1:
Private Sub CommandButton1_Click()
Unload Me
End Sub
(2) VBA in Modul1:
Sub Button_Procedure()
Call UserForm1.Show(vbModeless)
If CommandButton1 = True Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
The VBA goes through but it does not enter the value 1 into Cell A1.
What do I need to modify to achieve the desired result?
I strongly suggest to follow the steps in this article: Rubberduck: UserForm1.Show
Nevertheless, a simple and dirty implementation could be as follows:
The form's code behind:
Add an event to raise when the OK-Cancel button has been pressed passing a boolean value indicating either to proceed or not:
Public Event OnClose(ByVal bool As Boolean)
Private Sub CmdOK_Click()
RaiseEvent OnClose(True)
End Sub
Private Sub CmdCancel_Click()
RaiseEvent OnClose(False)
End Sub
A simple wrapper class:
Here, we just instantiate the form and listen to the OnClose() event.
Option Explicit
Private WithEvents objForm As UserForm1
Private m_flag As Boolean
Public Function Show() As Boolean
Set objForm = New UserForm1
objForm.Show ' No vbModeless here, we want to halt code execution
Show = m_flag
End Function
Private Sub CloseForm()
Unload objForm
Set objForm = Nothing
End Sub
Private Sub objForm_OnClose(ByVal bool As Boolean)
m_flag = bool
CloseForm
End Sub
Calling the wrapper class:
Sub Something()
Dim bool As Boolean
With New FormWrapper
bool = .Show
End With
MsgBox "Should I proceed? " & bool
End Sub
With reference to this question I used a Boolean variable:
(1) Code in UserForm1:
Private continue_procedure As Boolean
Private Sub CommandButton1_Click()
continue_procedure = True
Unload Me
End Sub
Function check_procedure() As Boolean
UserForm1.Show
check_procedure = continue_procedure
End Function
(2) Code in Modul1:
Sub Button_Procedure()
If UserForm1.check_procedure() = True Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub

Global variable not accessible in different sub?

I declared projname globally at the top of the module 1. It is assigned in the userform, and is successfully accessed in the createWB sub. However, when I go to access it in the addWindow sub also in module 1, it becomes empty (""). I'm unsure of why this is happening because I thought that since the variable was globally declared I should be able to access it in any sub.
Module 1
Option Explicit
Public outputWorkbook As Workbook
Public globalcounter As Integer
Public projname As String
Public projnum As String
createWB()
Dim uf2 As New UserForm2
uf2.Show
Set outputWorkbook = Workbooks.Add(xlWBATWorksheet)
outputWorkbook.SaveAs Filename:=Environ("userprofile") & "\Desktop\" &
Replace(projname, " ", "") & ".xlsx"
outputWorkbook.Activate
Range("B3") = projname
Range("B4") = projnum
End Sub
addWindow()
Workbooks(Replace(projname, " ", "") + ".xlsx").Activate
End Sub
Userform Code
Public Sub CommandButton1_Click()
projname = Me.TextBox1.Text
projnum = Me.TextBox2.Text
Me.Hide
End Sub
Cells B3 and B4 are assigned the correct value, but the addWindow() line causes a subscript out of range error. When I test it with Debug.Print, I see that projname = "". I also simply tried outputWorkbook.Activate, which did not work either.
Avoid Global Pollution
Unless there is a really good reason to use them, try to avoid global variables. We want to avoid polluting the global namespace. Captain Planet warned us of that.
Instead, try passing your parameters through your various methods as they are needed. This helps prevent errors, makes your code easier to follow, and utilizes composition.
Using your userform to store and expose your properties
Try to instantiate your userform using a With statement so that you have a captured instance of it where you have access to its various properties that you expose. In your case ProjectName and ProjectNumber.
Additionally, there should be a property to check if the userform was canceled or the X button was pressed.
You userform would look something like this:
Option Explicit
Private cancelled As Boolean
Public Property Get ProjectName() As String
ProjectName = TextBox1.Value
End Property
Public Property Get ProjectNumber() As Long
ProjectNumber = TextBox2.Value
End Property
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelled
End Property
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
OnCancel
End If
End Sub
Private Sub OnCancel()
cancelled = True
Hide
End Sub
Instantiating the userform
Here is the example of now calling your userform (P.S. Change the name from Userform2). Notice we are capturing our instance of your userform using the With block. Within this block, we have access to the properties we exposed: ProjectName, ProjectNumber, IsCancelled.
Private Sub createWB()
With New UserForm2
.Show
If Not .IsCancelled Then
' Do neccessaray steps here...
' You have access to ProjectName and Project number.
' Pass this to your addWindow method.
addWindow .ProjectName
End If
End With
End Sub
The ProjectName now can be accessed from your userform and passed as a parameter to you addWindow method.
Private Sub addWindow(ByVal projName As String)
Workbooks(Replace(projName, " ", "") + ".xlsx").Activate
End Sub
For more information on using userforms in this way see this helpful Rubberduck Blog Post.
could you try using Module1 as prefix? , jus like in this code
Public Sub CommandButton1_Click()
Module1.projname = Me.TextBox1.Text
Module1.projnum = Me.TextBox2.Text
Me.Hide
End Sub

How to make a variable public for all the workbook

I put this code into the module page
Option Explicit
Dim correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub
Then when I call these 2 subs from a sheet my variable correct never switch to True
Private Sub CommandButton4_Click()
Call setCorrect
Call checkCorrectTrue
End Sub
Because you're using Dim correct As Boolean it is only available in that module. To be able to use the variable across modules you need to declare it using Public
Try using
Option Explicit
Public correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub

Option buttons on form for database connection

I have 2 forms and on the first form, I have a label to open the second form. The second form is all about database connection options. I have 2 frame controls. The first frame is named frOpts and has 3 option buttons: Prod,Cert and Dev. The second frame is frType with SQL connection options either login using ad or using a SQL login. When SQL login is selected, 2 text boxes for username and pass are enabled and go from grey to white. I also have a module that is doing all of the work. How do I pass these parameters to the Module that is building the SQL connection string? Do I use a global variable in the module? Also, how would I send the right parameter from the option buttons to my main module? I've googled as much as I consider appropriate before asking. I was using a simple test with a command button on this form with this code:
Private Sub cmdOK_Click()
Select Case frOpts
Case 1: MsgBox "Prod"
Case 2: MsgBox "Cert"
Case 3: MsgBox "Dev"
End Select
'Me.Hide
End Sub
but that is giving me a Type mismatch. Not sure what I'm doing wrong. So my question is: What is the best way to do what I'm trying to achieve.
Update
Here is what I ended up with:
UserForm1
Private Sub CommandButton1_Click()
UserForm2.Show (False)
End Sub
Private Sub UserForm_Activate()
Me.Show (False)
End Sub
UserForm2
Public xOpt As Integer
Public xTxt As String
Public xType As Integer
Public xTxt2 As String
Private Sub CommandButton1_Click()
Select Case xOpt
Case 1: xTxt = "Prod"
Case 2: xTxt = "Cert"
Case 3: xTxt = "Dev"
End Select
Select Case xType
Case 1: xTxt2 = "AD login"
Case 2: xTxt2 = "SQLLogin"
End Select
Module1.BuildString xTxt, xTxt2
Unload UserForm2
End Sub
Private Sub OptionButton1_Click()
xOpt = 1
End Sub
Private Sub OptionButton2_Click()
xOpt = 2
End Sub
Private Sub OptionButton3_Click()
xOpt = 3
End Sub
Private Sub OptionButton4_Click()
xType = 1
End Sub
Private Sub OptionButton5_Click()
xType = 2
End Sub
Module1
Public Sub BuildString(sOpts As String, sType As String)
sConn = sOpts & " " & sType
Debug.Print sConn
End Sub
Thanks for your Help Justin. I couldn't have done it without you.
Set both forms to ShowModal = False
In the first form:
Private Sub CommandButton1_Click()
UserForm2.Show
UserForm2.xOpts = frOpts
End Sub
In the second form:
Public xOpts As Integer
Private Sub CommandButton1_Click()
MsgBox xOpts
End Sub
Edit per comments:
Try this as a full mock up in a new sheet:
Both with ShowModal=False and 1 Command Button.
UserForm1:
Private Sub CommandButton1_Click()
UserForm2.Show (False)
UserForm2.xOpt = "3"
End Sub
UserForm2:
Public xOpt As Integer
Private Sub CommandButton1_Click()
Debug.Print xOpt
End Sub

Right-click withevents works on source .xlsm but not on .xlam addin

I've made a couple of macros that run through right click menu button based on the cell value. Typically, if I right click on cell with value 'XYZ', the menu button shows as 'Run macro for XYZ' and then does a bunch of operations: show a couple of user forms, run an SQL query, show and format result data.
On the original .xlsm file, on 'Thisworkbook' I have the following code:
Public WithEvents mxlApp As Application
Public WithEvents mxlSh As Worksheet
Private Sub mxlApp_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As
Boolean)
... (do stuff here) ...
End Sub
...
Private Sub Workbook_Open()
Call AutoExec
End Sub
...
On a separate module, I have the following function used to set my event handler
Public Sub AutoExec()
Set mxlApp = Application
Set ColectionOfMxlEventHandlers = New Collection
ColectionOfMxlEventHandlers.Add mxlApp
Debug.Print ThisWorkbook.Name & " Initialized"
End Sub
The problem: on the original .xlsm file, the code works fine: every time I right-click on a cell which meets certain criteria, I get the 'Run macro for XYZ' and all is fine.
Once I save the file as .xlam and load it as addin, the code won't work.
I have been looking everywhere on the internet and here and couldn't figure out how to resolve this issue.
EDIT:
After modifying the code as kindly suggested by creamyegg, this is what I have:
In class module clsAppEvents:
Private WithEvents mxlApp As Excel.Application
Private Sub Class_Initialize()
Set mxlApp = Excel.Application
End Sub
Private Sub mxlApp_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim cBut As CommandBarButton
On Error Resume Next
Call CleanMenu
If Len(Target.Value) = 8 Then
MyId = Target.Value
With Application
Set cBut = .CommandBars("Cell").Controls.Add(Temporary:=True)
End With
With cBut
.Caption = "Run SQL Query for " & MyId
.Style = msoButtonCaption
.FaceId = 2554
.OnAction = "CallGenericQuery"
End With
End If
With Application
Set cBut = .CommandBars("Cell").Controls.Add(Temporary:=True)
End With
With cBut
.Caption = "Columns_Select"
.Style = msoButtonCaption
.FaceId = 255
.OnAction = "CallShowHide"
End With
On Error GoTo 0
End Sub
in Thisworkbook class I have
Public m_objMe As clsAppEvents
Private Sub Workbook_Open()
Set m_objMe = New clsAppEvents
Debug.Print ThisWorkbook.Name & " Initialized"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Call CleanMenu
On Error GoTo 0
Set m_objMe = Nothing
End Sub
Private Sub Workbook_Deactivate()
Call CleanMenu
End Sub
MyId is defined as a public string in the main module containing the CallShowHide and callGenericQuery subs
The issue sounds like your WithEvents is still in your ThisWorkbook Class? What you need to do is create a new class and then instantiate an instance of this on the Workbook_Open() event of your add-in. For example:
New Class (clsAppEvents):
Private WithEvents mxlApp As Excel.Application
Private Sub Class_Initialize()
Set mxlApp = Excel.Application
End Sub
Private Sub mxlApp_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
...
End Sub
Add-in ThisWorkbook Class:
Private m_objMe As clsAppEvents
Private Sub Workbook_Open()
Set m_objMe = New clsAppEvents
End Sub
Private Sub WorkbookBeforeClose(Cancel As Boolean)
Set m_objMe = Nothing
End Sub

Resources