If FileExists(settings) Then frmMain.chkStartup.Value = lngReturn
End Function
Saving the data into the files work just not the input although, there is no error the value does not get sent to the check box.
Thanks,
Nightwalker
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Dim strSection As String
Dim strKey As String
Dim strValue As String
Dim strDefault As String
Dim strFile As String
Dim lngLength As Long
lngReturn = GetPrivateProfileString(strSection, _
strKey, _
strDefault, _
strValue, _
lngLength, _
strFile)
Of course it won't return anything because you've just declared your Variables and passed them as they are without setting anything to them, so they're empty!!!!!
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
This works for the current data in the file. However, I am doubtful it will work if I expand the data in the file.
vb Code:
Public Function ReadINI()
Dim strSection As String
Dim strKey As String
Dim strValue As String
Dim strDefault As String
Dim strFile As String
Dim lngLength As Long
Dim ff As Integer
Dim entry
lngReturn = GetPrivateProfileString(strSection, _
strKey, _
strDefault, _
strValue, _
lngLength, _
strFile)
ff = FreeFile
settings = App.Path & "\settings.ini"
Open settings For Input As #ff
Do Until EOF(ff)
Input #ff, entry
Loop
Close #ff
startup = Right(entry, 1)
If FileExists(settings) Then frmMain.chkStartup.Value = startup
End Function
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
As pointed out, you need to fill in the values. Plus the lngReturn is not what you're
after. It provides the length of the buffer returned in the lpReturnedString param.
Here's an easy to use class for reading/writing config files:
I just found dee-u's example is the FAQ section. I will look into it further tomorrow and see if it is what I am after or not.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
because having the "-1" there causes an "Invalid procedure call or argument" error. However, when I run that code nothing appears in the msgbox even though there is data in the ini file.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
If you put 'Option Explicit' at the top of the Declarations Section I think you'll find that the variable 'cap' is not defined. If that is the case, nothing will be returned when you read the file (that's also why you got the "Invalid Procedure Call or Argument" message.)
Sorry, cap was defined I just had the definition up the top of the module because I thought it might be needed by both WIni and RIni.
vb Code:
Dim cap As String
That is in the declarations section of the module in question. It is on line 16 of code in post #6.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
The variables 'cap' and 'Settings' only get set when you write to the ini file with WIni. If you call RIni before WIni both values are Null.
Yeah, although, what I was hoping to do was load the data into the program on load, thus populating the different controls, etc with the values from the ini file. However, as you say if the ini is blank then it will not work. Any ideas on how I could solve this?
Perhaps you should set those values in the Form_Load event.
At the moment I have the call to the RIni sub in the load_form event and the call to the WIni sub in the command button click event.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Well, the variable 'Settings' is going to be constant so it can be defined in the Declarations Secttion as you have done. Allocate it's value in the Form_Load event. If you want to read values for controls from the .ini file then, again, the Form_Load event is probably the place to do it
Code:
Public Function RIni(strKey As String) As String
RIni = ReadIni(Settings, "Load", strKey)
End Function
Private Sub Form_Load()
Settings = App.Path & "\settings.ini"
chkstartup.Value = RIni(chkstartup.Caption)
' etc
Unfortunately I receive a type mismatch error on this line:
vb Code:
chkstartup.Value = RIni(chkstartup.Caption)
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Strange, it works for me. Does App.Path & "\settings.ini" exist ? If so, does it have a 'Load' section and a Key of whatever chkstartup.Caption is ?
Yes, it does exist! Here is some data that is currently in the ini file:
[Load]
Load on Startup=1
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
I'm sure we all have our different ways of handling INI files. I have one that's pretty lightweight and easy to use. It even lets you pass an INI Section as a parameter, retrieve values with defaults, etc.
It's a small INI DOM Class named Teeni. Example:
Code:
Private Sub Main()
Dim Settings As Teeni
Dim Num As Integer
Dim Key As Teeni
Dim S As String
Set Settings = New Teeni
With Settings
If .Load("my.ini") Then
With !Window
!Top.Value = 10
!Left.Value = 120
!Width.Value = 500
!Height.Value = 320
End With
With ![Saved Images]
![1].Value = "abc.gif"
![2].Value = "xyz.gif"
Num = 3
.Item(Num).Value = "another.gif"
End With
.Save
End If
End With
MsgBox "Settings![Saved Images]![2].Value=""" & Settings![Saved Images]![2].Value & """"
MsgBox "Settings!Window!Top.Value=""" & Settings!Window!Top.Value & """"
'Example using a default Value:
MsgBox "Settings![Saved Images]![37].Value(""default.gif"")=""" _
& Settings![Saved Images]![37].Value("default.gif") & """"
For Each Key In Settings![Saved Images]
S = S & Key.Name & "=""" & Key.Value & """" & vbNewLine
Next
MsgBox S
End Sub
Although, it appears as if you have hard-coded the values in to the main sub instead of loading them from the ini.
This is I have in the main sub and it appears to work. However, as can see I have hard-coded the value.
vb Code:
Option Explicit
Private Sub Main()
Dim Setting As Teeni
Dim Num As Integer
Dim Key As Teeni
Dim S As String
Set Setting = New Teeni
With Setting
If .Load("settings.ini") Then
With ![Load]
![1].Value = "1"
End With
.Save
End If
End With
'Example using a default Value:
For Each Key In Setting![Load]
S = S & Key.Name & "=""" & Key.Value & """" & vbNewLine
Next
MsgBox S
End Sub
That works! Although, I am unsure why it works? I manually changed the "Load on Startup=" to "0" to verify this. Also, is it possible to do without having the name of the sections, etc?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
The values were being set in Main if the INI file wasn't there.
I'm not sure what you mean about doing without Section names, INI files are a 2-level deep hierarchy and a Key name must be qualified by its Section. Are you looking for another file format?
You can have something like:
Code:
Dim LoadSection as Teeni
Set LoadSection = Settings![Load]
MsgBox LoadSection![1].Value
I mean is there a way to do it without having to hard-code the section names such as [Load], etc but install retrieve that from the ini file itself?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
I don't think there's a way of doing that using the APIs. The nearest you can get is to read an entire Section, but you still have to pass the section name. The alternative would be to read the .ini file as a flat file and parse the data accordingly. That would complicate the entire process.
Why do you not want to specify the Section? At some point in time your code is going to have to know what Section it's processing and the key / value pairs in it, so rather than 'discovering' it why not specify it.?
Why do you not want to specify the Section? At some point in time your code is going to have to know what Section it's processing and the key / value pairs in it, so rather than 'discovering' it why not specify it.?
I was thinking that for a big project that having to specify all the sections would be a hassle not to mention having to modify the code where I needed to add a new section or key into the ini file.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Dim Section As Teeni
Dim Key As Teeni
Dim S As String
For Each Section In Settings
For Each Key In Section
S = S & Section.Name & "." & Key.Name & "=" & Key.Value & vbNewLine
Next
Next
MsgBox S
S = S & Section.Name & "." & Key.Name & "=" & Key.Value & vbNewLine
Next
Next
MsgBox S
End Sub
Edit:
Well, I seem to encounter the same problem whether I use the above code or the code in post #15 and that is how do I redefine the value of S?
Example if I do:
vb Code:
For Each Section In Settings
For Each Key In Section
S = Key.Value
MsgBox S
Next
Next
All the values will equal the initial value say 1.
Last edited by Nightwalker83; Oct 4th, 2011 at 02:40 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
I went over the code and I found that when I "cleaned up" a version of Teeni.cls to make the small demo package posted above I had put in a version of the Item() function that does not fit with the rest of the Class. So let's just call it a bug, which should be fixed now.
The change deals with the difference between passing a String "name" and a non-String (usually Integer or Long) "index" value to the Item() function.
So for this corrected version let's just load and dump this INI file:
Code:
[Section 1]
Key1=Tom
Key2=Dick
Key3=Harry
[Second Section]
1=First
2=Second
3=Third
[Last]
This is it=That's all, folks!
To do that we'll use:
Code:
Private Sub Main()
Dim Settings As Teeni
Dim Section As Teeni
Dim Key As Teeni
Dim S As Integer
Dim K As Integer
Set Settings = New Teeni
Settings.Load "test.ini"
For Each Section In Settings
Debug.Print Section.Name
For Each Key In Section
Debug.Print Spc(4); Key.Name; "="; Key.Value
Next
Next
Debug.Print
For S = 1 To Settings.Count
Debug.Print Settings.Item(S).Name
For K = 1 To Settings.Item(S).Count
With Settings.Item(S).Item(K)
Debug.Print Spc(4); .Name; "="; .Value
End With
Next
Next
End Sub
There we are "dumping" the entire DOM using two different approaches, a For... Each and a For index=1 To Count approach.
The results I get are:
Code:
Section 1
Key1=Tom
Key2=Dick
Key3=Harry
Second Section
1=First
2=Second
3=Third
Last
This is it=That's all, folks!
Section 1
Key1=Tom
Key2=Dick
Key3=Harry
Second Section
1=First
2=Second
3=Third
Last
This is it=That's all, folks!
Unfortunately when I attempt to run the attached project it stops as soon as I do.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
The code works perfectly for me. I just un-zipped it and ran it as is.
Yeah, I just realized that but it is a blink and you miss it case.
Edit:
Showing the values in a msgbox rather than printing to the debug windows solves that problem.
This works I just need to add the new controls as I add them.
vb Code:
Public Function RIni(Settings As String)
Dim config As Teeni
Dim Section As Teeni
Dim Key As Teeni
Dim S As Integer
Dim K As Integer
Set config = New Teeni
config.Load "Settings.ini"
For Each Section In config
'For Each Key In Section
'Next
Next
For S = 1 To config.Count
For K = 1 To config.Item(S).Count
With config.Item(S).Item(K)
If K = 1 Then frmMain.chkStartup.Value = .Value
If K = 2 Then frmMain.Check1.Value = .Value
End With
Next
Next
End Function
Last edited by Nightwalker83; Oct 5th, 2011 at 04:34 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Loading values from ini file does nothing
So why not:
Code:
config.Load "Settings.ini"
With Config(1) 'Grab 1st Section. Trust the Force, Luke!
frmMain.chkStartup.Value = ![Whatever I called this].Value(optional default value)
frmMain.Check1.Value = ![This key's name].Value(optional default value)
End With
Re: [RESOLVED] Loading values from ini file does nothing
Ah, I will try that.
Edit:
This is what I put it does not work, no error, nothing.
vb Code:
config.Load "Settings.ini"
With config(1) 'Grab 1st Section. Trust the Force, Luke!
frmMain.chkStartup.Value = ![Load].Value(0)
frmMain.Check1.Value = ![Load].Value(0)
End With
I think I will just stick to what I have because it works and less hassle in the long run.
Last edited by Nightwalker83; Oct 5th, 2011 at 06:11 PM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Loading values from ini file does nothing
oops, no I put the section by mistake. This works.
vb Code:
Set config = New Teeni
config.Load "Settings.ini"
For Each Section In config
config.Load "Settings.ini"
With config(1) 'Grab 1st Section. Trust the Force, Luke!
frmMain.chkStartup.Value = ![Load on startup].Value(0)
frmMain.Check1.Value = ![Test].Value(0)
End With
Next
Although it defeats the purpose of automatically retrieving the sections, etc such as in post #26.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Loading values from ini file does nothing
Why am I receiving error with this code:
vb Code:
'Unused for Key nodes. Marked as Procedure ID = -4!
Public Function NewEnum() As IUnknown
Set NewEnum = mItems.[_NewEnum]
End Function
Object variable or with block variable not set when I use:
vb Code:
Call RIni(Settings)
I can't figure out why?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Loading values from ini file does nothing
Also:
Code:
Set config = New Teeni
config.Load "Settings.ini"
For Each Section In config
config.Load "Settings.ini"
With config(1)
frmMain.chkStartup.Value = ![Load on startup].Value(0)
frmMain.Check1.Value = ![Test].Value(0)
End With
Next
Is kind of weird. Why try to Load again inside the loop?
Shouldn't it be:
Code:
Set config = New Teeni
config.Load "Settings.ini"
With config(1)
frmMain.chkStartup.Value = ![Load on startup].Value(0)
frmMain.Check1.Value = ![Test].Value(0)
End With
Or even:
Code:
Set config = New Teeni
config.Load "Settings.ini"
With config(1)
chkStartup.Value = ![Load on startup].Value(vbUnchecked)
Check1.Value = ![Test].Value(vbUnchecked)
End With
Re: [RESOLVED] Loading values from ini file does nothing
Originally Posted by dilettante
What are RIni and Settings?
The NewEnum only gets used with For... Each.
RIni is the function I posted in post #26. If the NewEnum is only suppose to get used with For...Each I think there is another bug in the code? Well, either removing this code completely or commenting out solves the problem.
vb Code:
For Each Section In config
'For Each Key In Section
'Next
Next
Originally Posted by dilettante
Also:
Code:
Set config = New Teeni
config.Load "Settings.ini"
For Each Section In config
config.Load "Settings.ini"
With config(1)
frmMain.chkStartup.Value = ![Load on startup].Value(0)
frmMain.Check1.Value = ![Test].Value(0)
End With
Next
Is kind of weird. Why try to Load again inside the loop?
Shouldn't it be:
Code:
Set config = New Teeni
config.Load "Settings.ini"
With config(1)
frmMain.chkStartup.Value = ![Load on startup].Value(0)
frmMain.Check1.Value = ![Test].Value(0)
End With
Or even:
Code:
Set config = New Teeni
config.Load "Settings.ini"
With config(1)
chkStartup.Value = ![Load on startup].Value(vbUnchecked)
Check1.Value = ![Test].Value(vbUnchecked)
End With
The code I am using at the moment if not the same as in post #26 is:
vb Code:
Public Function RIni(Settings As String)
Dim config As Teeni
Dim Section As Teeni
Dim Key As Teeni
Dim S As Integer
Dim K As Integer
Set config = New Teeni
config.Load "Settings.ini"
For S = 1 To config.Count
For K = 1 To config.Item(S).Count
With config.Item(S).Item(K)
If K = 1 Then frmMain.chkStartup.Value = .Value
End With
Next
Next
End Function
Now that I removed the for each section in config part it works as it is suppose to.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Loading values from ini file does nothing
I know what you are saying but I trying to make so there is as little editing to do with outside files as possible.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Loading values from ini file does nothing
Yeah, I will data file instead.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Loading values from ini file does nothing
Almost all my programs use INI files now for settings.
If I don't have multiple sections then its normally just a single section like settings or general.
This is what I use to read the values:
In my case I just read everything as a String.
Code:
Private 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
Public Enum INI_TYPE
ENBOOL = 3
ENLONG = 2
ENINT = 1
ENSTR = 0
End Enum
Public Function ReadFromINI(sSection As String, sKey As String, sDefault As String, sFile As String, Optional ByVal pType As INI_TYPE = ENSTR) As Variant
Dim lret As Long, sValue As String
On Error GoTo ErrHandler
If Dir(sFile) <> "" Then
sValue = Space(1024)
lret = GetPrivateProfileString(sSection, sKey, sDefault, sValue, Len(sValue) - 1, sFile)
ReadFromINI = Trim(Left(sValue, lret))
Select Case pType
Case 1, 2
If IsNumeric(ReadFromINI) = False Or Len(ReadFromINI) = 0 Then
ReadFromINI = CInt(sDefault)
Else
ReadFromINI = CInt(ReadFromINI)
End If
Case 3
If IsNumeric(ReadFromINI) = False Or Len(ReadFromINI) = 0 Then
ReadFromINI = CBool(sDefault)
Else
ReadFromINI = CBool(ReadFromINI)
End If
Case Else
If Len(ReadFromINI) = 0 Then
ReadFromINI = CStr(sDefault)
Else
ReadFromINI = CStr(ReadFromINI)
End If
End Select
End If
Exit Function
ErrHandler:
Debug.Print err.Description
err.Clear
Resume Next
End Function
Example:
Code:
[settings]
title=Demo Program
quality=3
fullscreen=1
posleft=
Code:
Private Sub Form_Load()
Dim sFile As String
sFile = App.Path & "\settings.ini"
'// String test
Dim sTitle As String
sTitle = ReadFromINI("settings", "title", "My Program", sFile)
'// Number test
Dim tmpSpeed As String, iSpeed As Integer
tmpSpeed = ReadFromINI("settings", "quality", "3", sFile)
iSpeed = IIf(IsNumeric(tmpSpeed), tmpSpeed, 3)
'// True or False test
Dim tmpFullscreen As String, bFullscreen As Boolean
tmpFullscreen = ReadFromINI("settings", "fullscreen", "0", sFile)
bFullscreen = IIf(IsNumeric(tmpFullscreen), tmpFullscreen, False)
'// Form Position test
Dim tmpLeft As String
tmpLeft = ReadFromINI("settings", "posleft", "", sFile)
If IsNumeric(tmpLeft) Then
Form1.Left = tmpLeft
Else
Form1.Left = (Screen.Width / 2) - (Form1.Width / 2)
End If
Me.Caption = sTitle
Debug.Print iSpeed
If bFullscreen Then Form1.WindowState = 2
End Sub