Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving listbox items using GetSetting? 2

Status
Not open for further replies.

r3b00t

Technical User
Aug 13, 2002
45
0
0
GB
Ive got a project where the user is asked for three responses, these responses, when the appropriate button is clicked, are added to a listbox on a second form and the user can view the items by pressing a button to display the second form.

The problem i have is, when the program is closed and re-opened the items that were in the listbox are now gone.

Ive been messing about with some code using the SaveSetting and GetSetting functions but havent been able to get it right yet.

Heres what ive got so far

Private Sub Form_Unload(Cancel As Integer)
SaveSetting App.EXEName, "Settings", "LIST", list1.ListIndex
End Sub

and on the second form

Private Sub Form_Load()
For X = 0 To list1.ListCount - 1
list1.AddItem (X)
Next
On Error GoTo aaron:
list1.ListIndex = GetSetting(App.EXEName, "Settings", "LIST")
aaron:
End Sub

Can any of you guys please help?!?

Cheers

r3b00t

 
Instead of
Code:
Private Sub Form_Unload(Cancel As Integer)
SaveSetting App.EXEName, "Settings", "LIST", list1.ListIndex
End Sub

try
Code:
Private Sub Form_Unload(Cancel As Integer)
dim lngCount as Long
dim strListthing as String 
for lngCount = 1 to 3
strListthing = strListthing & List1.List(lngCount)& ","
next
SaveSetting App.EXEName, "Settings", "LIST", strListthing
End Sub
This will store "listitem1,listitem2,listitem3," as a string

To get it all back
instead of
Code:
Private Sub Form_Load()
For X = 0 To list1.ListCount - 1
        list1.AddItem (X)
    Next
On Error GoTo aaron:
    list1.ListIndex = GetSetting(App.EXEName, "Settings", "LIST")
aaron:
End Sub
try
Code:
Private Sub Form_Load()
dim lngCount as Long
dim strListthing as String 
dim aryList as String(3)
strListthing = GetSetting(App.EXEName, "Settings", "LIST")
aryList = split(strListthing,",")

For lngCount = 0 to 2       
    list1.AddItem aryList(lngCount)
Next
End Sub
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Thanx for the reply, but i get an expected array error message when trying to run the program.

Any ideas???

Cheers

r3b00t
 
Where does the error highlight? Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Hi


firstly i get a syntax error here:

dim aryList as String>>>>>(3)<<<<<

Then it highlights here:

list1.AddItem >>>>>>aryList<<<<<(lngCount)


Any ideas???

Cheers

r3b00t
 
Sorry - I did it too fast

Dim aryList(3) as String
My apologies Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Ok i changed the code to:

Private Sub Form_Load()
Dim lngCount As Long
Dim strListthing As String
Dim aryList(3) As String
strListthing = GetSetting(App.EXEName, &quot;Settings&quot;, &quot;LIST&quot;)
aryList(3) = Split(strListthing, &quot;,&quot;)

For lngCount = 0 To 2
List1.AddItem aryList(3)(lngCount)
Next
End Sub

but get the message 'Type mismatch', so then i removed the (3) from List1.AddItem aryList(3)(lngCount) and aryList(3) = Split(strListthing, &quot;,&quot;) but when running the program i get the message 'Cant assign to array'.

Any ideas???

Cheers

r3b00t

 
That will teach me to test before posting!

I've run this and it works. My apologies for the messing around

Private Sub Form_Load()
Dim lngCount As Long
Dim strListthing As String
Dim aryList() As String
strListthing = GetSetting(App.EXEName, &quot;Settings&quot;, &quot;LIST&quot;)
aryList = Split(strListthing, &quot;,&quot;)
For lngCount = 0 To 2
List1.AddItem aryList(lngCount)
Next
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim lngCount As Long
Dim strListthing As String
For lngCount = 0 To 2
strListthing = strListthing & List1.List(lngCount) & &quot;,&quot;
Next
SaveSetting App.EXEName, &quot;Settings&quot;, &quot;LIST&quot;, strListthing
End Sub
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Hi

Sorry about this and thanx for taking time to help me but after trying to run the program i get message 'subscript out of range' and the line highlighted is List1.AddItem aryList(lngCount)

Cheers

r3b00t

 
That sounds all very complicated. I think it would be easier to store the values in a textfile or a database, Access or so...
 
It works fine for me! Can you put a break point in the code and make sure that you are getting the right string back from the getsettings.

Just try one thing first. Rem out the form load code, run the prog, putting your 3 data items in and then close the form. This will make sure that your reg setting string is full. Then unrem the form load code and try again please. Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Hi,
&quot;Subscript out of range&quot; means your trying to access an array element to isn't available, or &quot;out of range&quot;.

Try this...
Code:
Private Sub Form_Load()
Dim lngCount As Long
Dim strListthing As String
Dim aryList() As String
Dim lngUpper As Long           'New line

strListthing = GetSetting(App.EXEName, &quot;Settings&quot;, &quot;LIST&quot;)
aryList = Split(strListthing, &quot;,&quot;)
lngUpper = UBound(aryList)     'New line
For lngCount = 0 To lngUpper   'Revised
  List1.AddItem aryList(lngCount)
Next
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim lngCount As Long
Dim strListthing As String
Dim lngUpper As Long               'New line
lngUpper = List1.ListCount - 1     'New line

For lngCount = 0 To lngUpper       'Revised
strListthing = strListthing & List1.List(lngCount) & &quot;,&quot;
Next

SaveSetting App.EXEName, &quot;Settings&quot;, &quot;LIST&quot;, strListthing
End Sub

I added lngUpper Long variable to hold the upper bound (or maximum array index from UBound function) coz List1 item count may vary.

Hope this helps!
 
First of all, thanx very much guys for taking the time to help this edgit!

Johnwm: I tried remming out the Form Load which produces no errors at runtime but the values are not being set in the registry

alphanytz: I also tried your code but no registry entries are saved.

Any more ideas guys?

Thanx again guys for trying to help!

r3b00t
 
If the values are not being set in the registry, is the FormUnload event not running? Can you stick a msgbox in the formunload just before endsub.

MsgBox strListThing
End Sub

Run your app. then close the form. Make sure that the msgbox comes up with the right string in it.
If it does, use regedit to search for the string.
Then we will know where the problem lies. Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Hi johnwm

The msgbox doesnt appear when i close the program, what does this mean???

Thanx again

r3b00t
 
It means that the FormUnload event isn't being run. Very odd. You don't have an End statement anywhere do you? Because that will stop the prog immediatley, with no unload event. Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Yeah in fact i do! Ive got it for on the exit command.

What do you suggest i do?

r3b00t

 
Remove the End statement and replace with
Code:
Dim f As Form
For Each f In Forms
Unload f
Next

MSDN says:
The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
John thats great, thanx alot man!
Only thing is, that when the program is opened again after closing a blank listbox item is inserted into the list.
Can i remove this?

And thanx again for all your help and patience, i owe you a pint =D
 
Are you using my code or Alphanytz's variation?

I notice in A's code that he using
For lngCount = 0 To lngUpper 'Revised

He has made lngUpper = ubound(aryList) which is of course 3 and will give an blank line in the listbox Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top