A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes
A problem reguarding saving from listboxes A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
Go Back  Xtreme Visual Basic Talk > > > A problem reguarding saving from listboxes


Reply
 
Thread Tools Display Modes
  #1  
Old 04-16-2004, 04:43 AM
ReaperFett ReaperFett is offline
Freshman
 
Join Date: Feb 2004
Posts: 48
Default A problem reguarding saving from listboxes


Okay, I want to have a simple system where two listboxes interact. One shows the MemberID, Forename and Surname from Member and the other shows BookID and title. When you select something from each list, you can click add loan and it will use a table called Loan and add the MemberID and BookIDs to columns (They are made via AutoNumber in Access, they save to a number datatype), and then input today's date. The idea is I can then use this table with foreign keys to show the full details in that relationship.

However, when ever I attempt a loan, it gibes me an error saying "Run-time error '3420': Object invalid or no longer set", highlighting "rs3!MemberID = rs!MemberID" in Private Sub MakeLoanButton_Click().

Basically, what am I doing wrong? All I need is the relevent ID.

As an aside, is there any way to make it so that a message popup appears if you try and add a loan when you haven't selected from both columns, stopping it from adding?


The code follows:
Code:
Private Sub Form_Load()
Set MyDataBase = OpenDatabase(App.Path & "\Library.mdb")
Set rs = MyDataBase.OpenRecordset("select * from Member Order by MemberID")
Set rs2 = MyDataBase.OpenRecordset("Book")
Set rs3 = MyDataBase.OpenRecordset("Loan")


Do While Not rs.EOF
MemberList.AddItem rs!MemberID & " -  " & rs!Forename & " " & rs!Surname
rs.MoveNext
Loop
rs.Close

Do While Not rs2.EOF
BookList.AddItem rs2!BookID & " -  " & rs2!BookName
rs2.MoveNext
Loop
rs2.Close
End Sub

Private Sub MakeLoanButton_Click()
Reply = MsgBox("Are you sure you wish to make this loan?", 1, "Make Loan?")
If Reply = 1 Then
    rs3.AddNew
    rs3!MemberID = rs!MemberID
    rs3!BookID = rs2!BookID
    rs3!DateStarted = DateSerial(Year(Date), Month(Date), Day(Date))
    rs3.Update
       
    Exit Sub
 End If
End Sub

Thanks in advance
Reply With Quote
  #2  
Old 04-16-2004, 05:37 AM
chrisap chrisap is offline
Newcomer
 
Join Date: May 2003
Location: Greece, Athens
Posts: 20
Default

Recordsets are closed when you try to copy value from the one to the other. I had the same problem once in the past. In the 2nd procedure try to reopen recordsets. You are having the selected values from your listbox so make specific queries when you are reopening them. I hope i help you...
Reply With Quote
  #3  
Old 04-16-2004, 07:59 AM
MKoslof's Avatar
MKoslofA problem reguarding saving from listboxes MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

If both recordsets are closed you can't update them. And you should get the values you want, and store them in variables, then CLOSE the recordset. You don't want three open recordsets for no reason. Then within the recordset you want to update, just insert the variable value, such as:

Code:
With rs3 .AddNew .Fields("Field1") = strString .Fields("Field2") = strValue .update End With rs3.close Set rs3 = Nothing
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #4  
Old 04-16-2004, 02:29 PM
ReaperFett ReaperFett is offline
Freshman
 
Join Date: Feb 2004
Posts: 48
Default

Can I ask, why don't I want open recordsets for no reason?



And that doesn't fully do it I don't think. The problem is that I store into the listbox more than one piece of data, so one says "2 - JAMES BOND" or whatever, and all I need to take from that to store is the 2.
Reply With Quote
  #5  
Old 04-16-2004, 03:05 PM
LaVolpe's Avatar
LaVolpeA problem reguarding saving from listboxes LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

You don't have to open the recordsets again. I see what you are talking about. However, you can't update rs3 from those recordsets, you want to update them from the listbox:
rs3!MemberID = 1st portion of MemberList
rs3!BookID = 1st portion of Booklist
rs3!DateStarted = DateSerial(Year(Date), Month(Date), Day(Date))
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
  #6  
Old 04-16-2004, 03:20 PM
ReaperFett ReaperFett is offline
Freshman
 
Join Date: Feb 2004
Posts: 48
Default

Yeah, that sounds right. How would you go about getting that portion?
Reply With Quote
  #7  
Old 04-16-2004, 03:27 PM
LaVolpe's Avatar
LaVolpeA problem reguarding saving from listboxes LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

You're lucky... normally parsing strings is cumbersome at times, but since all you want are numbers and your listbox is always formatted like "# - text", it is easy. This wouldn't work in all cases; but it will in this case.
MemberID=Val(MemberList.Text)
BookID=Val(BookList.Text)

Last Note: you probably would want to ensure at least one item from each list is selected, if you haven't already. If a listbox has not items selected, then the value returned by Val(listbox.text) will always be zero.
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
  #8  
Old 04-16-2004, 03:42 PM
ReaperFett ReaperFett is offline
Freshman
 
Join Date: Feb 2004
Posts: 48
Default

And because the name part is in text, it wont include any numbers which are within that?


That's great, thanks!
Reply With Quote
  #9  
Old 04-16-2004, 03:53 PM
LaVolpe's Avatar
LaVolpeA problem reguarding saving from listboxes LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

Quote:
Originally Posted by ReaperFett
And because the name part is in text, it wont include any numbers which are within that?


That's great, thanks!
True in this case
Example: Val("125 - 1313 Mockingbird Lane") still gets evaluated to 125
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
  #10  
Old 04-16-2004, 03:55 PM
ReaperFett ReaperFett is offline
Freshman
 
Join Date: Feb 2004
Posts: 48
Default

That'll do perfect then


One other quick question. Is there any need for "Set rs3 = Nothing"? Why would I need to make this blank?
Reply With Quote
  #11  
Old 04-16-2004, 04:01 PM
LaVolpe's Avatar
LaVolpeA problem reguarding saving from listboxes LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

Force of habit. VB is suppose to properly dispose of all variables/objects when it closes; but most coders know that may not always happen for various reasons. When it doesn't happen, you have a memory leak, a file left open somewhere, or a host of other things. My rule of thumb is that if you use the SET keyword, then somewhere in your code use it again, this time with the = Nothing.
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
  #12  
Old 04-16-2004, 04:07 PM
ReaperFett ReaperFett is offline
Freshman
 
Join Date: Feb 2004
Posts: 48
Default

Okay then, thanks
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with saving an excel file naveen1182002 Excel 3 02-04-2004 05:32 AM
Problem with saving selected data VB-wanna-b Database and Reporting 2 02-02-2004 03:50 AM
Saving as problem inoripoff101211 General 4 01-19-2004 04:15 PM
Saving a file as bmp problem Perfect_Man General 4 01-16-2004 10:17 PM
A friend of mine's problem! (Saving and adding) Roselene General 3 09-25-2000 08:34 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
 
A problem reguarding saving from listboxes
A problem reguarding saving from listboxes
 
-->