I would be very grateful for some help, as I am not a programmer:
I have a form that comprises a series of text boxes, many of which I would lke to replace with drop-down lists. To test this, I put a combo box on my form, but I got stuck on how to populate it, so I searched the previous postings on the Lounge and came up with the perfect solution, posted by Hans, (who has often helped me in the past), but I clearly don’t understand how to exactly use it.
The solution Hans posted is:
You could store the information in a text file, for example in a .ini file. Word can read .ini files using System.PrivateProfileString.
An ini file consists of one or more sections denoted by [SectionName] and entries of the form EntryName=Value. For example:
[Employees]
Count=3
Item1=John
Item2=Mary
Item3=David
Code to populate a combo box could look like this:
Dim i As Integer
Dim n As Integer
Const strPath = “C:Settings.ini”
n = System.PrivateProfileString(strPath, “Employees”, “Count”)
For i = 1 To n
Me.cboEmployees.AddItem System.PrivateProfileString(strPath, “Employees”, “Item” & i)
Next i
The ini file can be created and edited in any text editor, for example Notepad.
Regards,
Hans
I set up a test ‘settings.ini’ file that contains:
[Size]
Count=2
Item1=10
Item2=12
I then double-clicked on the combo box on my form to display the relevant code and added a modified version of the code written by Hans, as follows:
Private Sub CboxSize_Change()
Dim i As Integer
Dim n As Integer
Const strPath = “C:Settings.ini”
n = System.PrivateProfileString(strPath, “Size”, “Count”)
For i = 1 To n
Me.CBoxSize.AddItem System.PrivateProfileString(strPath, “Size”, “Item” & i)
Next i
End Sub
I must have done something wrong, as the combo box appears empty when I run the AutoNew macro.
Your help would really be appreciated.