What does this mean? I got it recently when writing a small database app.
Open "Users.dat" For Append As #1
Close #1
Open "Pass.dat" For Append As #1
Close #1
cboUser.List = GetStuff("Users", "", "")
Is what errors and my function for GetStuff is globally declared as:
Function GetStuff(FileName As String, appname As String, key As String) As String
Dim sFile As String
Dim sDefault As String
Dim lSize As Integer
Dim l As Long
Dim sUser As String
sUser = Space$(128)
lSize = Len(sUser)
sFile = App.Path & "\" & FileName & ".dat"
sDefault = ""
l = GetPrivateProfileString(appname, key, sDefault, sUser, lSize, sFile)
sUser = Mid(sUser, 1, InStr(sUser, Chr(0)) - 1)
GetStuff = sUser
End Function
Can anyone tell me what's wrong?
Quote from: Dyndrilliac on November 13, 2003, 09:13 AM
What does this mean? I got it recently when writing a small database app.
cboUser.List = GetStuff("Users", "", "")
I'm thinking you probably need to pass an index to List?
Quote from: Dyndrilliac on November 13, 2003, 09:13 AM
cboUser.List = GetStuff("Users", "", "")
Why not try
cboUser.additem GetStuff("Users", "", "")
Quote from: Adron on November 13, 2003, 11:48 AM
I'm thinking you probably need to pass an index to List?
Erm, explain?.
Btw - I still error with .AddItem
Quote from: Dyndrilliac on November 14, 2003, 08:24 AM
Quote from: Adron on November 13, 2003, 11:48 AM
I'm thinking you probably need to pass an index to List?
Erm, explain?.
Btw - I still error with .AddItem
I don't know, but for the lists I use, List is typically an array, and you need to do something like List(0) = "bla bla"... But, then you need to have added an item already, so the best answer for you is probably to use some method that adds an item.
Sounds like he's probably confused over the differences in .List and .AddItem. Typically you use AddItem to append a new item to the ComboBox. Use .List(index) to read or modify an existing entry in the ComboBox.
If you want to read or modify the currently-selected item, you can use .Text property.
Some more examples:
cboUsers.AddItem "[vL]Adron" 'adds a user to the combo's list
cboUsers.ListIndex = 0 'selects the first entry in the combo's list
cboUsers.Text = "[vL]Grok" 'changes selected user name
cboUsers.RemoveItem 5 'removes the 6th entry in the list
cboUsers.List(3) = "[vL]Skywing" 'changes user name of 4th entry in list
MsgBox cboUsers.List(0) 'displays user in combo list first slot
Hope this helps.
Well, I triedcboUser.additem GetStuff("Users", "", "")
ad it didn't work - any others that Add Items without there already being an item?
Couple questions. Why are you opening the files for Append, then immediately closing them? As far as I know this has no effect whatsoever.
Secondly, has users.dat already been created so that GetStuff() can retrieve information from it? Why don't you test GetStuff()'s output by adding
Debug.Print GetStuff("Users", "", "")
to make sure that you're getting what you want in the first place? Your .additem code is correct as far as adding items to the combobox is concerned.
Quote from: Dyndrilliac on November 14, 2003, 05:41 PM
Well, I triedcboUser.additem GetStuff("Users", "", "")
ad it didn't work - any others that Add Items without there already being an item?
You do realize that GetStuff("users", "", "") will always return an empty string?
Private Sub Command1_Click()
cboUser.AddItem GetStuff("Users", "names", "1")
cboUser.ListIndex = cboUser.ListCount - 1
End Sub
Private Function GetStuff(FileName As String, appname As String, key As String) As String
Dim sFile As String
Dim sDefault As String
Dim lSize As Integer
Dim l As Long
Dim sUser As String
sUser = Space$(128)
lSize = Len(sUser)
sFile = App.Path & "\" & FileName & ".dat"
sDefault = ""
l = GetPrivateProfileString(appname, key, sDefault, sUser, lSize, sFile)
sUser = Mid(sUser, 1, InStr(sUser, Chr(0)) - 1)
GetStuff = sUser
End Function
Quote from: Stealth on November 15, 2003, 02:13 AM
Couple questions. Why are you opening the files for Append, then immediately closing them? As far as I know this has no effect whatsoever.
Secondly, has users.dat already been created so that GetStuff() can retrieve information from it? Why don't you test GetStuff()'s output by adding
Debug.Print GetStuff("Users", "", "")
to make sure that you're getting what you want in the first place? Your .additem code is correct as far as adding items to the combobox is concerned.
Well for one, My idea that was the Append would create the file if it didn't already exist, but I did make sure it does.
Grok, dont mean to sound ignorant, but um, what? I wasn't thinking in terms of string, i just reasoned that since the users.dat has the user names, the combobox should get it's list from there...
The combo box will just sit on your form until you tell it otherwise. If you want to add items to the list, you must add each string using the AddItem method of the ComboBox.
To do that, you need to iterate through your keys using GetPrivateProfileString, (or GetPrivateProfileSection, then split that into an array and walk through it), adding each value as the new string item.
For this to work, GetPrivateProfileString (or Section) expects an INI-style textfile, as such:
[names]
name1=[vL]Grok
name2=[vL]Adron
name3=[vL]Skywing
To get the value of 'name2', you'd do this:
key = "name2"
l = GetPrivateProfileString(appname, key, sDefault, sUser, lSize, sFile)
Isn't there some way to have GetPrivateProfileString return a list of all the value names found in a section, or all the sections found in an ini file? Passing in null or something like that?
Quote from: Adron on November 15, 2003, 06:33 PM
Isn't there some way to have GetPrivateProfileString return a list of all the value names found in a section, or all the sections found in an ini file? Passing in null or something like that?
Not that I'm aware of, did you see it in MSDN? AFAIK, you use GetPrivateProfileSection for that.
Quote from: Grok on November 15, 2003, 07:06 PM
Not that I'm aware of, did you see it in MSDN? AFAIK, you use GetPrivateProfileSection for that.
Hmm, no, it was just a vague memory I had. I haven't used those functions in a long time.
edit:
MSDN says:
Quote
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpDefault, // default string
LPTSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // initialization file name
);
Parameters
lpAppName
[in] Pointer to a null-terminated string that specifies the name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.
lpKeyName
[in] Pointer to the null-terminated string specifying the name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.
Ah, makes sense. I wrote an INI class that has a GetSection() method which takes advantage of that feature.
I used to work with INI-File type functions alot. I'll post some of my old code, if i can find it.
Quote from: Grok on November 15, 2003, 08:50 PM
Ah, makes sense. I wrote an INI class that has a GetSection() method which takes advantage of that feature.
I was thinking maybe the original poster was confused about that, thinking that if he passed in empty strings, he'd get everything. And that assigning it to List would automagically parse it out for him.
Quote from: Adron on November 16, 2003, 07:09 AM
Quote from: Grok on November 15, 2003, 08:50 PM
Ah, makes sense. I wrote an INI class that has a GetSection() method which takes advantage of that feature.
I was thinking maybe the original poster was confused about that, thinking that if he passed in empty strings, he'd get everything. And that assigning it to List would automagically parse it out for him.
That was my original intention - I wanted to pick a file to tretrieve all the data from so i could have unlimited usernames and compare the username they chose with the password on the same line of the other file that would hold the passwords.