 |
|
 |
Gary Level: Sage
 Registered: 27-11-2005 Posts: 50
|
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 |
|
|
Keithuk Level: Big Cheese

 Registered: 02-11-2006 Posts: 19
|
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 |
|
|
|
|
 |
 |