PDA

View Full Version : Just VBA - pass info from user form to module?



Andrew74
04-29-2019, 02:07 AM
Hello

I'm pretty new to VBA & I'd like to pass user information from the form to the module - this is what I have so far:

Very early stages - i understand the processing should be done in the module, not the userform.
I can't pass the value from the userform to the module:

The final screen shot shows the message box returning the value aa from the userform when the command button is clicked. However click exit the module message box only shows " _" ww & aa are both missing?
(aa is in the userform, ww is just a copy of aa)

2415524156

The overall aim is to print a list of drawings overnight.
The user enters a list of drawing numbers, the application then opens them, prints as pdf's, closes & opens the next.

This 'early stage' is to pass the input to the form, save the number & build up a list.

I'm also trying to find help here too:
https://forums.autodesk.com/t5/inventor-customization/inventor-vba-userform-to-open-a-file-then-close-after-action/td-p/8753389


Regards

Andrew

Dave
04-30-2019, 05:52 AM
Top of module code...

Public aa as String
Public ww as String
HTH. Dave

Andrew74
04-30-2019, 06:35 AM
Hello Dave

Thanks for taking a look.

After some further study i've discovered passing values between the user form & the module isn't straight forward so concentrating on the userform for now.Another discovery is having a variable = variable & looping number is difficult too (impossible?)The idea is for the user to populate the textboxes with drawing numbers - then iterate through them. open idw1, (defer updates on), print to pdf, closenextopen idw2, (defer updates on), print to pdf, close What alternatives are there?

Dave
04-30-2019, 10:30 AM
If U put variables as Public at the top of a module (as previously posted) then they are available to the whole project. If just U Dim a variable at the top of a module then it available to all the subs/code in the module. Same for Dim a variable at the top of userform code. I U dim a variable in a sub (or command button code) then it is only available to that sub. "Fullpath" is an XL term that cannot be used as a variable name (at least not without errors). U also have Option Explicit at the top of your code (good) and therefore need to declare (Dim) all of your variables somewhere. What U want to achieve is doable and really should not be that difficult. Dave

Paul_Hossler
05-03-2019, 11:01 AM
I got this from someone here in the forums, but I forgot who gave it to me

Put a Public Function in the UserForm code and have it return data to the calling macro




Public Function Value_Ret( _
Optional strTitle As String = "My Custom Input", _
Optional strPrompt As String = "Enter Something", _
Optional vntDefault As Variant = vbNullString _
) As Variant

With Me
.Caption = strTitle
.lblPrompt.Caption = strPrompt
With .txtInput
.Value = vntDefault
.SelStart = 0
.SelLength = Len(vntDefault)
End With
.Show

If Len(.txtInput.Value) > 0 And Not .txtInput.Value = vntDefault Then
Value_Ret = .txtInput.Value
Else
Value_Ret = False
End If
End With
Unload frmInput
End Function



and call it like this, probably in a standard module



Option Explicit
Sub example02()
MsgBox frmInput.Value_Ret(, , "old text")
End Sub