VB and VBA Users Source Code: Returning parameters from the command line
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning parameters from the command line
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, November 29, 2001
Hits:
842
Category:
Visual Basic General
Article:
The following code will retrieve parameter values from the command line (an example is included in the code header). 'Purpose : Returns a parameter value from a command line. 'Inputs : sParamName The name of the parameter to return the value of. ' [sDefault] The default value to return if the parameter is not found. ' [sItemSeperator] The text which seperates each parameter in the command line. ' [sValueSeperator] The text which seperates the value from the parameter name. 'Outputs : Returns the requested parameter value from the command line, or a default if the parameter doesn't exist. 'Author : Andrew Baker 'Date : 15/01/2001 22:34 'Example : If your command line was "Database = MyDatabase, Server = MyServer, UserID = Andrew" ' Debug.Print CommandLineParam("Server","DefaultServer") ' Returns "MyServer" ' Debug.Print CommandLineParam("AdminMode","DefaultValue") ' Returns "DefaultValue", as this item is not in the command line 'Revisions : Function CommandLineParam(ByVal sParamName As String, Optional sDefault As String = "", Optional sItemSeperator As String = ",", Optional sValueSeperator As String = "=") As String Static bInitialised As Boolean, asValues() As String, lNumParams As Long Dim lPosStart As Long, lPosEnd As Long, asTempValues() As String Dim lThisValue As Long, sCommand As String, sThisParam As String On Error GoTo ErrFailed If bInitialised = False Then bInitialised = True sCommand = Command$ If Len(sCommand) Then 'Parse the command line parameters into a string array asTempValues = Split(sCommand, sItemSeperator) ReDim asValues(1 To 2, 1 To UBound(asTempValues) + 1) 'Remove empty parameters and copy values into a 2D array For lThisValue = 0 To UBound(asTempValues) sThisParam = Trim$(asTempValues(lThisValue)) If Len(sThisParam) Then lPosStart = InStr(1, sThisParam, sValueSeperator, vbTextCompare) If lPosStart Then lNumParams = lNumParams + 1 asValues(1, lNumParams) = UCase$(Trim$(Left$(sThisParam, lPosStart - Len(sValueSeperator)))) 'Remove extra asValues(2, lNumParams) = Trim$(Mid$(sThisParam, lPosStart + Len(sValueSeperator))) End If End If Next If lNumParams Then ReDim Preserve asValues(1 To 2, 1 To lNumParams) End If End If End If 'Find the item in the command line sParamName = UCase$(sParamName) 'Case insensative For lThisValue = 1 To lNumParams If asValues(1, lThisValue) = sParamName Then CommandLineParam = asValues(2, lThisValue) Exit For End If Next If lThisValue = lNumParams + 1 Then 'Did not find value, return default CommandLineParam = sDefault End If Exit Function ErrFailed: 'Return default Debug.Print "Error in CommandLineParam: " & Err.Description CommandLineParam = sDefault End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder