Output form data to a text ini file

M

Mat

Hi

I have a need to write to a ini file. I have a form with
text boxes and the user will type in the data and then
click on a button to write to the ini file. However, I
cannot get it to do what I want.

I have done following:

1. In a module in the db entered :

Option Compare Database

Declare Function GetPrivateProfileString Lib "kernel32"
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName
As String, ByVal lpKeyName As String, 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 String,
ByVal lpString As Any, ByVal lpFileName As String) As Long


2. In the code for the form:

Option Compare Database

Function ReadINI(Section, KeyName, filename As String) As
String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfileString(Section,
ByVal KeyName, "", sRet, Len(sRet), filename))
End Function

Function writeini(sSection As String, sKeyName As String,
sNewString As String, sFileName) As Integer
Dim r
r = WritePrivateProfileString(Section, KeyName,
sNewString, filename)
End Function
Private Sub Command1_Click()
Text1 = ReadINI("SMTP", "Server", "c:\program files\app"
& "\app.ini")
End Sub
Private Sub Command2_Click()
r = WritePrivateProfileString
("SMTP", "Server", "NewString", "c:\program files\app"
& "\app.ini")
End Sub

3. On the form I have 2 buttons (Command1, Command2) and
one text box (text1)

The first button reads the entry in the ini file - OK


4. The ini file has following:

[SMTP]
User Address=Bart
Server=aaaaaaaaa


I want to write/amend in the text box and then use button
2 (Command2) to write that value back to the ini file.

It will write "NewString" to the file - if it is entered
using " ", but I want it to take the value from the text1
textbox.


Can anyone help - it is driving me mad !

Thanks

Mat.
 
J

John Nurick

Hi Mat,

If you want to pass the contents of Text1 to
WritePrivateProfileString(), the Me keyword to refer to it, e.g.:

Private Sub Command2_Click()
Dim lngRetVal As Long
lngRetVal = WritePrivateProfileString( _
"SMTP", "Server", Me.Text1.Value, _
"c:\program files\app" & "\app.ini")
End Sub

By the way, this snippet worries me:

sRet = String(255, Chr(0))
ReadINI = Left(sRet, _
GetPrivateProfileString(Section, _
ByVal KeyName, "", sRet, Len(sRet), filename))

As I read it, it depends on Left() not evaluating sRet until after
GetPrivateProfileString() has modified it. Clearly this is what happens
in this version of VBA, but I'm not sure that it's documented behaviour
that can be counted on remain unchanged.

Either way, it's hard to understand, mainly because in the first line
you assign a value to sRet - but immediately afterwards you retrieve a
*different* value from it, with nothing apparently intervening. IMO the
code is much easier to read and therefore to maintain if revised along
these lines. It also declares Section and KeyName as Strings rather than
the Variants you had.

Public Function ReadINI(Section As String, _
KeyName As String, FileName As String) As String
'Fetches string from .ini file

Dim strBuffer As String
Dim lngKeyLength As Long

strBuffer = String(255, Chr(0))
lngKeyLength = GetPrivateProfileString(Section, KeyName, _
"", strBuffer, Len(strBuffer), FileName)
ReadINI = Left(strBuffer, LngKeyLength)
End Function



Hi

I have a need to write to a ini file. I have a form with
text boxes and the user will type in the data and then
click on a button to write to the ini file. However, I
cannot get it to do what I want.

I have done following:

1. In a module in the db entered :

Option Compare Database

Declare Function GetPrivateProfileString Lib "kernel32"
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName
As String, ByVal lpKeyName As String, 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 String,
ByVal lpString As Any, ByVal lpFileName As String) As Long


2. In the code for the form:

Option Compare Database

Function ReadINI(Section, KeyName, filename As String) As
String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfileString(Section,
ByVal KeyName, "", sRet, Len(sRet), filename))
End Function

Function writeini(sSection As String, sKeyName As String,
sNewString As String, sFileName) As Integer
Dim r
r = WritePrivateProfileString(Section, KeyName,
sNewString, filename)
End Function
Private Sub Command1_Click()
Text1 = ReadINI("SMTP", "Server", "c:\program files\app"
& "\app.ini")
End Sub
Private Sub Command2_Click()
r = WritePrivateProfileString
("SMTP", "Server", "NewString", "c:\program files\app"
& "\app.ini")
End Sub

3. On the form I have 2 buttons (Command1, Command2) and
one text box (text1)

The first button reads the entry in the ini file - OK


4. The ini file has following:

[SMTP]
User Address=Bart
Server=aaaaaaaaa


I want to write/amend in the text box and then use button
2 (Command2) to write that value back to the ini file.

It will write "NewString" to the file - if it is entered
using " ", but I want it to take the value from the text1
textbox.


Can anyone help - it is driving me mad !

Thanks

Mat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top