Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old August 4th, 2003, 11:59 AM
knrrao knrrao is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 1 knrrao User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
programming in MSAccess using VB

i am having two tabels in my database. both the tabels are having two fields, accountno and amt. second table is having some accountno matched to first tabel. i want to update the first table amount filed with the amount of second tabel for accountno matching with the accountno of second tabel
. i want to write code for a command click event .
i am giving the code written by me. Dim db As Database
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim rst3 As DAO.Recordset

Private Sub Command0_Click()
Set db = CurrentDb()
Set rst1 = db.OpenRecordset(" select * from aaa order by accountno")
Set rst2 = db.OpenRecordset("select * from bbb order by accountno")
rst1.MoveFirst
rst2.MoveFirst
MsgBox ("aaa" & rst1!accountno)
MsgBox ("bbb" & rst2!accountno)
Do While rst1.EOF

If rst2!accountno = rst1!accountno Then
rst2!amount = rst1!amount
rst1.Edit
rst1.Update
rst2.MoveNext
Else
rst1.MoveNext
End If

MsgBox (" bbb" & rst2!accountno)

Loop

If rst1.EOF Then
rst1.Close
rst2.Close
End If
End Sub


help in this regard may be highly appreciated.

my email id is : URL
URL
URL
URL

Reply With Quote
  #2  
Old August 4th, 2003, 10:39 PM
Mr. Ed Mr. Ed is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 Mr. Ed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
knrrao,
What is the relationship between the two tables? 1-1 or 1-many or many-many and what are the joining fields?

The following code should work assuming that there is zero or none matching records in table2.

rst1.MoveFirst
rst1.Edit
While NOT rst1.EOF
rst2.MoveFirst
While NOT rst2.EOF
If rst1!accountno = rst2!accountno Then
rst1!amount = rst2!amount
rst1.Update
MsgBox (" bbb" & rst2!accountno)
rst2.MoveLast
Else
rst2.MoveNext
End If
Wend
rst1.MoveNext
Wend

rst1.endEdit

Also you don't need the If clause:
If rst1.EOF Then
rst1.Close
rst2.Close
Endif

because you're at the end of the recordset anyway!
Cheers...Ed

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > programming in MSAccess using VB


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway