
When you do something like this:
Dim var1, var2, var3 As String
Only var3 is declared as a string variable. The rest are created as a Variant variable.
Not sure, but maybe that's part of the issue causing your problem.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am attempting to call a function that also passes by data in ByRef variable. Everything is a type string and the calling sub has the receiving variable set as type string. Yet I am getting the Byref mismatch message. The ByVal is the string variable passed in and the 3 Byref strings are passed out. Also, the function returned value is a String.
Images and a sample of the code are below. What am I doing wrong?
Public Sub CheckboxClicked(checkboxname As String)
Dim strFormPrefix, strAlphaNumText, strAlphaNumInteger, strFormName As String
strFormPrefix = ""
strAlphaNumText = ""
strAlphaNumInteger = ""
strFormName = GetFormNameFromPrefix(checkboxname, strFormPrefix, strAlphaNumText, strAlphaNumInteger)
Select Case strFormName
Case "CreateBurialSourceTableFormat"
CreateBurialSourceTableFormat\_CheckBoxClicked checkboxname
Case Else
End Select
MsgBox checkboxname
End Sub
Public Function GetFormNameFromPrefix(ByVal strPrefix As String, ByRef FormPrefix As String, ByRef AlphaNumText As String, ByRef AlphanumInteger As String) As String
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
When you do something like this:
Dim var1, var2, var3 As String
Only var3 is declared as a string variable. The rest are created as a Variant variable.
Not sure, but maybe that's part of the issue causing your problem.
To elaborate on theDBguy:
strFormPrefix is indeed a Variant, as you dimmed it.
Debug.Print VarType(strFormPrefix)
0 '0=vbEmpty
Passing such undefined variable to GetFormNameFromPrefix causes the error: VBA does not know what the data type will be at runtime, so it fails at compile time.
The fix is:
Dim strFormPrefix As String
Beyond that, you seem to be somewhat random about the use of ByRef/ByVal.
Here are some guidelines: