Results 1 to 26 of 26

Thread: [RESOLVED] Left$ Not Working?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Resolved [RESOLVED] Left$ Not Working?

    I found this code on these boards, and am at a loss here because for some strange reason I cannot compile this code. I can however run it normally error free. This is in a module by itself btw. The exact error given is.. Type-Declaration character does not match declared data type. What the heck? I've tried different variation, including the sister function Right$. But still no luck. The part that is highlighted is shown in bold in the code below. Any Ideas would be helpful. Sorry for the long code Registry w/ the API isn't real fun is it! Oh and BTW I searched through the first few pages of relevent information. Did multiple searches too.

    VB Code:
    1. ' Possible registry data types
    2. Public Enum InTypes
    3.    ValNull = 0
    4.    ValString = 1
    5.    ValXString = 2
    6.    ValBinary = 3
    7.    ValDWord = 4
    8.    ValLink = 6
    9.    ValMultiString = 7
    10.    ValResList = 8
    11. End Enum
    12. ' Registry value type definitions
    13. Public Const REG_NONE As Long = 0
    14. Public Const REG_SZ As Long = 1
    15. Public Const REG_EXPAND_SZ As Long = 2
    16. Public Const REG_BINARY As Long = 3
    17. Public Const REG_DWORD As Long = 4
    18. Public Const REG_LINK As Long = 6
    19. Public Const REG_MULTI_SZ As Long = 7
    20. Public Const REG_RESOURCE_LIST As Long = 8
    21. ' Registry section definitions
    22. Public Const HKEY_CLASSES_ROOT = &H80000000
    23. Public Const HKEY_CURRENT_USER = &H80000001
    24. Public Const HKEY_LOCAL_MACHINE = &H80000002
    25. Public Const HKEY_USERS = &H80000003
    26. Public Const HKEY_PERFORMANCE_DATA = &H80000004
    27. Public Const HKEY_CURRENT_CONFIG = &H80000005
    28. Public Const HKEY_DYN_DATA = &H80000006
    29. ' Codes returned by Reg API calls
    30. Private Const ERROR_NONE = 0
    31. Private Const ERROR_BADDB = 1
    32. Private Const ERROR_BADKEY = 2
    33. Private Const ERROR_CANTOPEN = 3
    34. Private Const ERROR_CANTREAD = 4
    35. Private Const ERROR_CANTWRITE = 5
    36. Private Const ERROR_OUTOFMEMORY = 6
    37. Private Const ERROR_INVALID_PARAMETER = 7
    38. Private Const ERROR_ACCESS_DENIED = 8
    39. Private Const ERROR_INVALID_PARAMETERS = 87
    40. Private Const ERROR_NO_MORE_ITEMS = 259
    41.  
    42. Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
    43. Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    44. Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
    45. Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
    46. Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    47. Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
    48. Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
    49. Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
    50. Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    51. Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
    52. Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    53. Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    54. Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
    55. Declare Function GetLastError Lib "kernel32" () As Long
    56. Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
    57. Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
    58. Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    59. Const LANG_NEUTRAL = &H0
    60. Const SUBLANG_DEFAULT = &H1
    61. Const REG_OPTION_BACKUP_RESTORE = 4     ' open for backup or restore
    62. Const REG_OPTION_VOLATILE = 1           ' Key is not preserved when system is rebooted
    63. Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
    64. Const STANDARD_RIGHTS_ALL = &H1F0000
    65. Const SYNCHRONIZE = &H100000
    66. Const READ_CONTROL = &H20000
    67. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    68. Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    69. Const KEY_CREATE_LINK = &H20
    70. Const KEY_CREATE_SUB_KEY = &H4
    71. Const KEY_ENUMERATE_SUB_KEYS = &H8
    72. Const KEY_NOTIFY = &H10
    73. Const KEY_QUERY_VALUE = &H1
    74. Const KEY_SET_VALUE = &H2
    75. Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    76. Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    77. Const KEY_EXECUTE = (KEY_READ)
    78. Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    79.  
    80.  
    81. ' This routine allows you to get values from anywhere in the Registry, it currently
    82. ' only handles string and double word values.
    83.  
    84. Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String
    85. Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String, td As Double
    86.   On Error Resume Next
    87.     lResult = RegOpenKeyEx(ByVal Group, ByVal Section, ByVal &O0, &H1, lKeyValue)
    88. '    If lResult > 0 Then
    89. '      Dim Buffer As String
    90. '      'Create a string buffer
    91. '      Buffer = Space(200)
    92. '      'Set the error number
    93. '      SetLastError lResult
    94. '      'Format the message string
    95. '      FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal &O0, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
    96. '      'Show the message
    97. '      MsgBox Buffer
    98. ' End If
    99.  sValue = Space$(2048)
    100.  lValueLength = Len(sValue)
    101.  lResult = RegQueryValueEx(ByVal lKeyValue, ByVal Key, ByVal 0&, ByVal lDataTypeValue, ByVal sValue, lValueLength)
    102.  If (lResult = 0) And (Err.Number = 0) Then
    103.    If lDataTypeValue = REG_DWORD Then
    104.       td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
    105.       sValue = Format$(td, "000")
    106.    End If
    107.    sValue = [b]Left$[/b](sValue, lValueLength - 1)
    108. Else
    109.   sValue = ""
    110. End If
    111. On Error GoTo 0
    112. lResult = RegCloseKey(lKeyValue)
    113. ReadRegistry = sValue
    114. End Function
    115.  
    116. ' This routine allows you to write values into the entire Registry, it currently
    117. ' only handles string and double word values.
    118. Public Sub WriteRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, ByVal ValType As InTypes, ByVal Value As Variant)
    119. Dim lResult As Long
    120. Dim lKeyValue As Long
    121. Dim InLen As Long
    122. Dim lNewVal As Long
    123. Dim sNewVal As String
    124. On Error Resume Next
    125.  RegCreateKeyEx Group, Section, 0, "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, lKeyValue, lResult
    126.  If ValType = ValDWord Then
    127.    lNewVal = CLng(Value)
    128.    InLen = 4
    129.    lResult = RegSetValueExLong(lKeyValue, Key, 0&, ValType, lNewVal, InLen)
    130.  Else
    131.    sNewVal = Value
    132.    InLen = Len(sNewVal)
    133.    lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen)
    134.  End If
    135.  lResult = RegFlushKey(lKeyValue)
    136.  lResult = RegCloseKey(lKeyValue)
    137. End Sub

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Left$ Not Working?

    Place that code in a Module, and then call it from your code.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    Sorry for sounding like a noobie, but how would I do that. I'm already calling the code like this from another form. It is in its own module.
    VB Code:
    1. WinKey = Replace(StrReverse(ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", "ProductID")), "-MEO", "")

  4. #4

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Left$ Not Working?

    Just outa curiosity, is this to run in VBA?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    Bruce... I got the code from dee-u. Not exactly which forum. That could pose an issue.
    Marty... Just using Left will throw "Expected array"

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Left$ Not Working?

    Here is a module that I posted the other night.

    http://www.vbforums.com/attachment.p...chmentid=38389

    Just place it in your app folder, and then add a module, and point to this one. It contains pretty much the same thing, if it's not identical

  8. #8

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    I am using VB6

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Left$ Not Working?

    Quote Originally Posted by DJHotIce
    Bruce... I got the code from dee-u. Not exactly which forum. That could pose an issue.
    Sorry, I was asking if that code was part of VBA (ie Access or Excel) app as I have
    recently lost the use of the Left$ function on a colegues computer that had the OS replaced recently - seems library dependant; hence my question.
    Also, you stated that you have had it running sucessfuly too.




    Bruce.

  11. #11

  12. #12
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Left$ Not Working?

    Quote Originally Posted by DJHotIce
    I am using VB6

    Ignore my last

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    is there any way to do that via the Debug. Sorry marty. I'm still learning VB. I wasn't real sure what VBA was. I thought that was a reference to the VB Suite.

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Left$ Not Working?

    Do what via debug?

    In any case if you want to find out if you have a Left variable just do Ctrl-f and look for Left.

    BTW I just did this and got the same error you did.

    VB Code:
    1. Dim Left As Integer
    2.  
    3.  Left$ = "xxx"

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    Quote Originally Posted by MartinLiss
    Do what via debug?
    Find out all the variables within my project. Add a watch or something right? I'm trying to learn. Please bear with me if that was completly WRONG! Ah.

    [edit]Nevermind, I see what your saying[/edit]

  16. #16

  17. #17
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Left$ Not Working?

    Try using Mid instead:
    VB Code:
    1. sValue = Mid$(sValue, lValueLength - 1)
    If you do have a variable named Left and possibly right as MartinLiss said, it's doubtful you would have a mid variable

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    Oh Also. I mentioned I also tried using the Right$ function as well. chemicalNova. Haven't used Mid Yet. Doubt I would use it as a var.

    [edit]Mid. Just spat out Errors on just a simple run through. Not a compile.
    Run Through = What's the Correct Name?[/edit]

  19. #19

  20. #20
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Left$ Not Working?

    For what its worth the code (adapted) works for me...

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    Well. I'd Prefer Not too. Here is the reason. I've already included the activation part. I could privatly mail it if Desired.

    I believe a problem could also be this... I have a Drive Serial Function (from the boards) and I haven't fully understood modules yet. So I kinda duplicated function inside of the form. I have removed those as of this writing (now) and still getting errors on that one Left function.

    [edit]I seriously hate having other people do the code for me. You know what I mean. I rather learn it and not seem like a stupid noobie moocher! But if you insist . Just want to get that point across.[/edit]

  22. #22

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    Recieved.

  24. #24
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Left$ Not Working?

    I found the source of your problem. VB is being confused by the many components you have selected. For example MSAdodcLib (among others) has a Left member and VB is making the wrong assumption about which one you are trying to use when you use Left$. You can correct that problem by doing sValue = VBA.Left$(sValue, lValueLength - 1) for example. A better way however would be to just remove the components that you have selected but that are not actually being used. To do that go to the Project|Components menu and check the Selected Items Only checkbox. Then go through the list of selected components and attempt to uncheck them. If you can uncheck a component that means that you are not actually using it. In your case you'll find that you can uncheck the 1st 7 and last 2 in the list. In the remaining 8 controls I see that you are using both Microsoft Windows Common Controls 5.0 and 6.0 and I don't think you should do that and I suggest you figure out where you are using the 5.0 control and replace it with the equivalent 6.0 control.

    Once you've done all that try and compile again. You'll get more errors but if you can't fix them then send me another zip file and we'll work on them together.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Left$ Not Working?

    Your a genius! Thanks a lot Marty. Props (+ Reputations) to everyone who posted as ya'll all contributed to fixing this problem.

  26. #26
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [RESOLVED] Left$ Not Working?

    That's a lot of code to read and write to the registry. Why don't you try the VBScript method? It's a hell of a lot less code and very easy to work with:

    VB Code:
    1. Private Function Registry_Read(Key_Path, Key_Name) As Variant
    2.    
    3.     On Error Resume Next
    4.    
    5.     Dim Registry As Object
    6.    
    7.     Set Registry = CreateObject("WScript.Shell")
    8.    
    9.     Registry_Read = Registry.RegRead(Key_Path & Key_Name)
    10.    
    11. End Function
    12.  
    13.  
    14. Private Sub Registry_Write(Key_Path As String, Key_Name As String, Key_Value As Variant, Optional Key_Type As String)
    15.    
    16.     On Error Resume Next
    17.  
    18.     '/////////////////////////////////////////////////////////////////////    
    19.     'Key Type list (Use within string, ex. "
    20.     '     REG_DWORD")
    21.     '----------------
    22.    
    23.     'REG_BINARY - This type stores the value as raw binary data. Most hardware component information is stored as binary data, and can be displayed in an editor in hexadecimal format.
    24.     'REG_DWORD - This type represents the data by a four byte number and is commonly used for boolean values, such as "0" is disabled and "1" is enabled. Additionally many parameters for device driver and services are this type, and can be displayed in REGEDT32 in binary, hexadecimal and decimal format, or in REGEDIT in hexadecimal and decimal format.
    25.     'REG_EXPAND_SZ - This type is an expandable data string that is string containing a variable to be replaced when called by an application. For example, for the following value, the string "%SystemRoot%" will replaced by the actual location of the directory containing the Windows NT system files. (This type is only available using an advanced registry editor such as REGEDT32)
    26.     'REG_MULTI_SZ - This type is a multiple string used to represent values that contain lists or multiple values, each entry is separated by a NULL character. (This type is only available using an advanced registry editor such as REGEDT32)
    27.     'REG_SZ - This type is a standard string, used to represent human readable text values.
    28.    
    29.     'Other data types not available through the standard registry editors include:
    30.    
    31.     'REG_DWORD_LITTLE_ENDIAN - A 32-bit number in little-endian format.
    32.     'REG_DWORD_BIG_ENDIAN - A 32-bit number in big-endian format.
    33.     'REG_LINK - A Unicode symbolic link. Used internally; applications should not use this type.
    34.     'REG_NONE - No defined value type.
    35.     'REG_QWORD - A 64-bit number.
    36.     'REG_QWORD_LITTLE_ENDIAN - A 64-bit number in little-endian format.
    37.     'REG_RESOURCE_LIST - A device-driver resource list.
    38.     '/////////////////////////////////////////////////////////////////////
    39.     Dim Registry As Object
    40.    
    41.     Dim Registry_Value As Variant
    42.    
    43.     Set Registry = CreateObject("WScript.Shell")
    44.    
    45.     Registry_Value = Registry_Read(Key_Path, Key_Name)
    46.    
    47.     If Key_Type = "" Then
    48.        
    49.         'REG_SZ is the default.
    50.        
    51.         Registry.RegWrite Key_Path & Key_Name, Key_Value
    52.        
    53.     Else
    54.        
    55.         Registry.RegWrite Key_Path & Key_Name, Key_Value, Key_Type
    56.        
    57.     End If
    58.    
    59. End Sub
    60.  
    61.  
    62. Private Sub Form_Activate()
    63.     'This is only an example of a registry entry.
    64.     Registry_Write "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN\", "Test", 1, "REG_DWORD"
    65.  
    66. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width