Renegade Projects Network Forums
I need help with Visual basic - Printable Version

+- Renegade Projects Network Forums (https://forums.renegadeprojects.com)
+-- Forum: General Forums (https://forums.renegadeprojects.com/forumdisplay.php?fid=1)
+--- Forum: The Renegade Inn (https://forums.renegadeprojects.com/forumdisplay.php?fid=2)
+--- Thread: I need help with Visual basic (/showthread.php?tid=1202)



I need help with Visual basic - Professor_Tesla - 20.12.2008

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


RE: I need help with Visual basic - Marshall - 20.12.2008

What if you put the "Private Declare Function ShellExecute" amongst the other Declare Functions, before your own routines?


RE: I need help with Visual basic - Professor_Tesla - 20.12.2008

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.


RE: I need help with Visual basic - Marshall - 21.12.2008

So make them Private, like ShellExecute. Or don't put them in an object module.


RE: I need help with Visual basic - Professor_Tesla - 21.12.2008

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?


RE: I need help with Visual basic - Marshall - 22.12.2008

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.


RE: I need help with Visual basic - Professor_Tesla - 22.12.2008

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.


RE: I need help with Visual basic - Marshall - 22.12.2008

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.


RE: I need help with Visual basic - Professor_Tesla - 23.12.2008

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.


RE: I need help with Visual basic - Marshall - 23.12.2008

There must be a problem with your string variable because that should work too.


RE: I need help with Visual basic - Professor_Tesla - 24.12.2008

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.


RE: I need help with Visual basic - Marshall - 24.12.2008

Option Explicit is your friend
Put that at the beginning of every form/module.