Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

ini files and comboboxes (VBA)

Novice:  I am trying to figure out how to get all the keys within a section of my ini file to populate a combobox on my user form.  Is this possible?  I was using text files for input.  But that required several text files for each form.  So I created an ini file, but I'm not sure how to retrieve the information.  Also, can I write to an ini file based on a user's selection?

For example the section is "author", the keys are a list of author names.

Thanks for any guidance.  -- JTG
0
jtaylerg
Asked:
jtaylerg
  • 2
  • 2
1 Solution
 
thunder_mooseCommented:
Hey there,

Could you give a bit more information? Perhaps paste the .ini file you created here? The code would be different depending on how it's formatted.

Thanks,
TM



0
 
jtaylergAuthor Commented:
Hi TM,

As I mentioned, I'm a novice and may not be formatting correctly.  But here's the text:

[Author]
A1="John Smith"
A2="Jane Doe"
A3="George Johnson"
A4="Steve Martin"

[Delivery]
D1="Via FedEx"
D2="Via Facsimile"
D3="Via U.S. Mail"
D4="Personal & Confidential"

[Closing]
C1="Sincerely"
C2="Very truly yours"
C3="Best regards"

Thanks -- JTG
0
 
thunder_mooseCommented:
Hey again,

Sorry for taking so long to get back to you, but it was weekend and all. Easy enough to do what you want. To read out the authors the code would look something like this:

Sub Populate_Combo()
Dim intFreeFile As Integer, intEntryLen As Integer, intQuotePos As Integer
Dim intLen As Integer
Dim sTemp As String

intFreeFile = FreeFile
Open "C:\YourIni.Ini" For Input As intFreeFile

While Not EOF(intFreeFile)
      Line Input #intFreeFile, sTemp
      If Len(sTemp) Then
         If InStr(sTemp, "[Authors]") Then
            Do
               Line Input #intFreeFile, sTemp
               intEntryLen = Len(sTemp)
               intQuotePos = InStr(sTemp, Chr(34)) + 1
               intLen = intEntryLen - intQuotePos
               If intLen < 0 Then intLen = 0
               sTemp = Mid(sTemp, intQuotePos, intLen)
               If Len(sTemp) And InStr(sTemp, "[") = 0 Then
                  ComboBox1.AddItem (sTemp)
               End If
            Loop Until InStr(sTemp, "[")
         End If
      End If
Wend

Close #intFreeFile

End Sub

To use it just put "Populate_Combo" in the Load event of your form. Please note that this a quick and dirty one, since I don't have a lot of time at the moment. It should do what you want though, just change the name of the combo to yours (if changed) and change ini file to the one you're using.

Best of luck,
TM
0
 
jtaylergAuthor Commented:
Thanks so much for getting back to me.  

I'm looking forward to giving this a try and dumping the multiple txt files.

Have a good week and thanks again.

JTG
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now