Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with Visual basic
#1
I need help with fixing something really annoying in my visual basic code. I found some code on the internet for working with .ini files, and I wanted to incorporate it into a program that I have written. Here is the relevant part of the code.
Code:
Declare Function GetPrivateProfileString Lib "kernel32" Alias _
                 "GetPrivateProfileStringA" (ByVal lpApplicationName _
                 As String, ByVal lpKeyName As Any, ByVal lpDefault _
                 As String, ByVal lpReturnedString As String, ByVal _
                 nSize As Long, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias _
                 "WritePrivateProfileStringA" (ByVal lpApplicationName _
                 As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
                 ByVal lpFileName As String) As Long
Public Function sGetINI(sINIFile As String, sSection As String, sKey _
                As String, sDefault As String) As String
    Dim sTemp As String * 256
    Dim nLength As Integer
    sTemp = Space$(256)
    nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, _
              255, sINIFile)
    sGetINI = Left$(sTemp, nLength)
End Function
Public Sub writeINI(sINIFile As String, sSection As String, sKey _
           As String, sValue As String)
    Dim n As Integer
    Dim sTemp As String
    sTemp = sValue
    'Replace any CR/LF characters with spaces
    For n = 1 To Len(sValue)
        If Mid$(sValue, n, 1) = vbCr Or Mid$(sValue, n, 1) = vbLf _
        Then Mid$(sValue, n) = " "
    Next n
    n = WritePrivateProfileString(sSection, sKey, sTemp, sINIFile)
End Sub
Sub InitProgram()
Dim sINIFile As String
Dim sBetaUpdatePath As String
Dim nCount As Integer
Dim i As Integer
'Store the location of the INI file
sINIFile = App.Path & "\WAAutorun.INI"
'Read the Beta Update Path from the INI file
sBetaUpdatePath = sGetINI(sINIFile, "WAAutorun", "BetaUpdatePath", "?")
If sBetaUpdatePath = "?" Then
      'No user name was present – ask for it and save for next time
      sBetaUpdatePath = InputBox$("Enter the location of the Beta Update:")
      writeINI sINIFile, "WAAutorun", "BetaUpdatePath", sBetaUpdatePath
End If
End Sub

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
The function at the bottom is part of the code that one of you (Marshall, I think) gave to me for opening a file with its default editor. For some stupid, extremely annoying reason, it won't put the function "ShellExecute" in a separate section to the Sub defined above it, so I get an error when I try to compile.MadBang head against wall
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#2
What if you put the "Private Declare Function ShellExecute" amongst the other Declare Functions, before your own routines?
Ever wondered what the hell is going on?
Believe me friend you're not the only one.
--Lysdexia

Check out Launch Base for RA2/YR - http://marshall.strategy-x.com
Also home to the Purple Alert mod, 1.002 UMP, and the YR Playlist Manager.
Reply
#3
That problem seems to have been solved, but now it won't compile because "Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules". It seems to have a problem with the first two "Declare Function"s that I posted above.
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#4
So make them Private, like ShellExecute. Or don't put them in an object module.
Ever wondered what the hell is going on?
Believe me friend you're not the only one.
--Lysdexia

Check out Launch Base for RA2/YR - http://marshall.strategy-x.com
Also home to the Purple Alert mod, 1.002 UMP, and the YR Playlist Manager.
Reply
#5
I made them private and the compile errors disappeared. However, my program doesn't do anything I wanted it to. I wanted to have a program with buttons that, when clicked, would open a file in a path obtained from an INI file. When the buttons are clicked, it goes into the program's directory and minmizes the window.
How do I not put the functions in an object module?
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#6
Not knowing what problems you're having, I suggest stepping thorugh the code and/or putting debugging in to see which functions aren't working as you expect.

Regarding object modules, I suspect it's complaining that you're putting the public declares in a form.
Just create a module and put your public functions in that.

I learned everything I know about VB by 25% trial and error, 75% googling.
Ever wondered what the hell is going on?
Believe me friend you're not the only one.
--Lysdexia

Check out Launch Base for RA2/YR - http://marshall.strategy-x.com
Also home to the Purple Alert mod, 1.002 UMP, and the YR Playlist Manager.
Reply
#7
I found out what the problem is. OpenLocation doesn't seem to want to take a string variable as an argument. It only seems to accept strings enclosed in quote marks.
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#8
I think it's more likely that there was a problem with your string var. I use Label1.Caption as the argument for OpenLocation and it works fine as long as the caption is valid.
Ever wondered what the hell is going on?
Believe me friend you're not the only one.
--Lysdexia

Check out Launch Base for RA2/YR - http://marshall.strategy-x.com
Also home to the Purple Alert mod, 1.002 UMP, and the YR Playlist Manager.
Reply
#9
It's strange, but I set the caption of a Label to the value of the string variable, used the caption label as an argument for the function, and it worked. Thanks for your help.
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#10
There must be a problem with your string variable because that should work too.
Ever wondered what the hell is going on?
Believe me friend you're not the only one.
--Lysdexia

Check out Launch Base for RA2/YR - http://marshall.strategy-x.com
Also home to the Purple Alert mod, 1.002 UMP, and the YR Playlist Manager.
Reply
#11
Spin eyes ← This is me.
I declared the string variable inside a private subroutine. Thus, as far as the sub I was trying to use it in as an argument for a function was concerned, it didn't exist. I should have remembered that from what I learned in C programming.
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#12
Option Explicit is your friend
Put that at the beginning of every form/module.
Ever wondered what the hell is going on?
Believe me friend you're not the only one.
--Lysdexia

Check out Launch Base for RA2/YR - http://marshall.strategy-x.com
Also home to the Purple Alert mod, 1.002 UMP, and the YR Playlist Manager.
Reply




Users browsing this thread: 1 Guest(s)