[RESOLVED] Mysterious Compiler Error
I've stumbled upon one of those mysterious coding bugs that drives all of us crazy. I was removing some code in Form A, adding some new code in Form B and Class A, and when I try to compile my project, I'm getting an error in a BAS file that I haven't touched in weeks. I use the following function to help me find information in the registry, and I don't see why any of my changes should've caused the compiler to blow up in an unrelated BAS module.
VB Code:
Private Function Query_ValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
On Error GoTo QueryValueExError
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
If lrc <> m_ERROR_NONE Then Error 5
Select Case lType
Case m_REG_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
If lrc = m_ERROR_NONE Then
vValue = Left$(sValue, cch) '<--- ERROR HERE
Else
vValue = Empty
End If
Case m_REG_DWORD:
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
If lrc = m_ERROR_NONE Then vValue = lValue
Case Else
lrc = -1
End Select
QueryValueExExit:
Query_ValueEx = lrc
Exit Function
QueryValueExError:
Resume QueryValueExExit
End Function
Compile Error: Type-declaration character does not match declared data type. Any ideas?
Re: Mysterious Compiler Error
it could be because:
- vValue is declared As Variant
- and Left$() returns String type
- and/or cch is declared As Long and Left$() is "expecting" Integer...
Try to use Left() instead or declare vValue as String at least.
Normaly those shoudn't be a problem unless cch exceeds Integer boundaries...
Re: Mysterious Compiler Error
Quote:
Originally Posted by RhinoBull
Try to use Left() instead or declare vValue as String at least.
Compile Error: Expected Array
Re: Mysterious Compiler Error
Oh, common ... that's not even funny ... :) Left(...) is a function so simply type Left without the "()" or type arguments inside the "(text, length)"...
Left instead of Left$ - if this makes more sense to you.
Re: Mysterious Compiler Error
Quote:
Originally Posted by RhinoBull
Oh, common ... that's not even funny ... :) Left(...) is a function so simply type Left without the "()" or type arguments inside the "(text, length)"...
Same error.
I agree with you that it is so simple that it shouldn't be choking on it. I'm starting to think I've done something elsewhere in the code that is causing the compiler to have a brainfreeze and forget that Left() isn't complicated and is getting an array: sValue (a string)
Re: Mysterious Compiler Error
Because vValue is a variant.... it's going to take on the datatype of what ever is passed in.... it should either be hard typed to a string, or it should be checked ahead of time to make sure it is a string.
-tg
RB - it was too funny.... *stifles a laugh*
Re: Mysterious Compiler Error
Try:
VB Code:
sValue = String(cch, " ")
or
Re: Mysterious Compiler Error
Close the project and reopen it. Do you still get the error?
Re: Mysterious Compiler Error
Quote:
Originally Posted by MartinLiss
Close the project and reopen it. Do you still get the error?
Yes, I still get the error.
Re: Mysterious Compiler Error
Quote:
Originally Posted by schoolbusdriver
Try:
VB Code:
sValue = String(cch, " ")
or
Both of your suggestions worked using vValue or sValue.
Re: Mysterious Compiler Error
VB Code:
Dim vValue
Dim cch As Long
Dim sValue As String
sValue = "this is a test"
cch = 4
vValue = Left$(sValue, cch)
MsgBox vValue
As you would expect the above works fine. Do you have a user-defines function called Left? In any case try using VBA.Left
Re: Mysterious Compiler Error
Quote:
Originally Posted by MartinLiss
As you would expect the above works fine. Do you have a user-defines function called Left? In any case try using VBA.Left
We have a winner! Here is the culprit code that--when renamed--resolved my problem:
VB Code:
Public Enum ceVScrollType
Invisible = 0
Right = 1 ' Renamed to tRight: Problem solved
Left = 2 ' Renamed to tLeft: Problem solved
End Enum
Crazy.
Re: [RESOLVED] Mysterious Compiler Error
Now that we've helped you, you can help us by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button which will let everyone know that you have your answer.
Re: [RESOLVED] Mysterious Compiler Error
Quote:
Originally Posted by MartinLiss
Now that we've helped you, you can help us by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button which will let everyone know that you have your answer.
It's already been done.