borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (Hooking api) New Topic New Poll Post Reply
AndreaVB Forum : API : Calling ini file (problem with code)
Poster Message
Gary
Level: Sage

Registered: 27-11-2005
Posts: 50

icon Calling ini file (problem with code)

Hi guys,

I'm having problem calling the .ini file and it returns the default value only.

My .ini file is as below:

[source]
dbname=MIMS DB
dbsource=GARYPC

My vbcode for calling it is as below:

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (lpAppName As String, lpKeyName As String, lpDefault As String, lpRetString As String, nSize As Long, lpFileName As String) As Long

Option Explicit

Sub DbConn_Main()

    On Error GoTo checkerror
    
    Dim dbname As String * 100
    Dim dbsource As String * 100
    Dim strpath As String
    Dim nbuffer As Long
    
    strpath = App.Path & "\mims.ini"
    
    nbuffer = GetPrivateProfileString("source", "dbname", "MIMS_CMS_Default", dbname, 100, strpath)
    nbuffer = GetPrivateProfileString("source", "dbsource", "MIMS_DB", dbsource, 100, strpath)
    
    'Database connection using recordset (frmModuleReg)...
    conn.ConnectionString = "Provider=SQLOLEDB.1;" & _
                            "Integrated Security=SSPI;" & _
                            "Persist Security Info=False;" & _
                            "Initial Catalog=" & Trim(dbname) & ";" & _
                            "Data Source=" & Trim(dbsource) & ";"
                
    MsgBox App.Path & " " & "\" & conn.ConnectionString
                
    'Open database connection when form loads...
    conn.Open
    
    Exit Sub
    
checkerror:
    Call MsgBox(Err.Description, vbInformation)
    
End Sub

Thank you...


Gary

28-12-2006 at 08:49 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 933
icon Re: Calling ini file (problem with code)

Hello. Time ago I posted the two functions into this speech in order to help managing INI files. Maybe you'll find it useful.
Hope it helps. Happy New Year.

[Edited by yronium on 28-12-2006 at 08:37 PM GMT]

____________________________
Real Programmer can count up to 1024 on his fingers

28-12-2006 at 07:36 PM
View Profile Send Email to User Show All Posts | Quote Reply
Keithuk
Level: Big Cheese


Registered: 02-11-2006
Posts: 19
icon Re: Calling ini file (problem with code)

Well want I do when I read an ini file is use a function.

Public Function GetIni(strSection As String, strKey As String, strDefault As String) As Variant

Dim Sp As String
Dim RetVal As Long
Dim strpath As String

strpath = App.Path & "mims.ini"
Sp = String(255, 0)
RetVal = GetPrivateProfileString(strSection, strKey, "", Sp, Len(Sp), strpath)
If RetVal = 0 Then
    GetIni = strDefault
Else
    GetIni = Left(Sp, InStr(Sp, Chr(0)) - 1)
End If

End Function

Then when you read data from the ini file you specify something like

'send the SectionName, KeyName, Default value
Me.Top = GetIni("Options", "Top", "150")
The 150 is a default value which it will return if it can't find a value because the ini file hasn't been written to before.  

Its done it again it won't allow me to add a slash  to the ini location.  

[Edited by Keithuk on 11-05-2008 at 12:12 PM GMT]

____________________________
http://www.martin2k.co.uk/forums/

I've been programming with VB for 14 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

11-05-2008 at 12:10 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 1022
icon Re: Calling ini file (problem with code)

Hi
What's it complaining about? GetPPS does support relative and absolute paths.
Where are you adding the ""?
strpath = app.path & "fred.ini"
strpath = "c:fred.ini"

both work for me


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

12-05-2008 at 05:47 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : API : Calling ini file (problem with code)
Next Topic (Hooking api) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Il portale per lui e lei | Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder