PDA

View Full Version : Need help with VBA code for passing variable from one program to another



marshallgrad
04-30-2014, 09:17 AM
START HERE WITH #1
==========================================================
Sub BUTCH_TEST()
' Generated by the Reflection Macro Recorder on 08-16-2013 07:55:22.98.
' Generated by Reflection for UNIX and OpenVMS 14.0.6.
' BUTCH JONES 8/16/2013
On Error GoTo ErrorHandler
Const NEVER_TIME_OUT = 0
Dim LF As String ' Chr(rcLF) = Chr(10) = Control-J
Dim CR As String ' Chr(rcCR) = Chr(13) = Control-M

LF = Chr(Reflection2.ControlCodes.rcLF)
CR = Chr(Reflection2.ControlCodes.rcCR)
With Session

.MsgBox "HELLO BUTCH, READY TO BEGIN? "

Call COMPUTER_NAME

.MsgBox "THE LABEL PRINTER YOU HAVE SELECTED IS: " & LABELPRINTER


End With
Exit Sub
ErrorHandler:
Session.MsgBox Err.Description, vbExclamation + vbOKOnly
' Recording stopped at 07:55:41.87.
End Sub
==========================================================

I want to start in the module (not sure what these are called? forgive me << Newbie) above and then transfer to the module below using the "CALL COMPUTER_NAME".

I then want the module below to identify the appropriate computer terminal I am using and assign
default items as listed. Then the program will revert back to the above module where the appropriate
"LABELPRINTER" will be displayed in the message box above.


Sub COMPUTER_NAME()
' Generated by the Reflection Macro Recorder on 03-21-2014 08:33:15.97.
' Generated by Reflection for UNIX and OpenVMS 14.0.6.
On Error GoTo ErrorHandler
Const NEVER_TIME_OUT = 0
Dim LF As String ' Chr(rcLF) = Chr(10) = Control-J
Dim CR As String ' Chr(rcCR) = Chr(13) = Control-M

LF = Chr(Reflection2.ControlCodes.rcLF)
CR = Chr(Reflection2.ControlCodes.rcCR)

With Session

'++++++++++++++++++++++++++++++++++++
'LIST OF COMPUTER NAMES THROUGHOUT THE LAB
'HEME DESK = '5'
'HEME MAIL = '6'
'DIFF DESK = '7'
'URINE DESK = '8'

'+++++++++++++++++++++++++++++++++++++

'===============================================================
COMPNAME = VBA.Environ("COMPUTERNAME")
.MsgBox COMPNAME

If COMPNAME = "5" Then
'MICROBIOLOGY PRINTER
LABELPRINTER = "C89$PRT"
DEFAULT_ACCESSION_AREA = "MICRO"
ElseIf COMPNAME = "6" Then
'BLOOD BANK LABEL PRINTER
LABELPRINTER = "BLAB1"
DEFAULT_ACCESSION_AREA = "BLOOD BANK"
ElseIf COMPNAME = "7" Then
'URINE LABEL PRINTER
LABELPRINTER = "UR11"
DEFAULT_ACCESSION_AREA = "URINE"
Else
'HEME/CHEM PRINTER
LABELPRINTER = "C93$PRT"
DEFAULT_ACCESSION_AREA = "HEME"
End If
'===============================================================

End With

Exit Sub
ErrorHandler:
Session.MsgBox Err.Description, vbExclamation + vbOKOnly
' Recording stopped at 08:34:32.97.
End Sub




Thanks for any help that can be given. Since I am a newbie, please be gentle...

RICVB
05-02-2014, 01:41 AM
I could suggest you to different ways

1) declare LABELPRINTER as "Public"
you should then add the variable declaration "Public LABELPRINTER as string" at the very top of any module belonging to your project. for instance you could place that declaration at the very top of the module containing Sub BUTCH_TEST()

2) declare LABELPRINTER in Sub BUTCH_TEST() and pass it to COMPUTER_NAME()
you should then
- add the variable declaration "dim LABELPRINTER as string" in any position of Sub BUTCH_TEST() preceeding the "Call COMPUTER_NAME" statement (but variable declaration are usually placed at the beginging of the module they are declared within)
- change the "Call COMPUTER_NAME" statement into "Call COMPUTER_NAME(ByRef LABELPRINT)"

and I'd add the following pieces of advice
a) as a good programming practice I'd recommend the 2nd choice, since it gives you more control over the variables: "Public" variables could be modified anywhere in your project and this may happen where you wouldn't it to

b) the "ByRef" keyword could be avoided since VBA takes it as the default choice. this, against the "ByVal" keyword that has to be specified for the arguments declaration in the sub calling statament if you want them to be passed and used in the called sub only without their changes affecting their values outside it.
but it's good programming practice to alay specify the keyword so as to help remembering you're doing the right choice

c) always place "Option Explicit" at the very top of any module
this will help you keep tracking the correct declaration and use of variables
for instance, were those two subs you posted the only code of your project (which I believe not), any undeclared variable (like LABELPRINTER) would be assumed as "variant" by default. which not only leads to more memory usage (this should not be a relevant issue nowadays) but may not be the proper variable type you are willing to deal with and this could bring in problems when trying to process it in your code while thinking it were specific variable type ("string", "integer", "double", and so on)

hope this helps