![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have seen a number of posts on the subject but I am unable to get them to work. I have been unable to find any information on the error that I am experiencing either. Here is the situation: I have two tables, let's say tblA and tblB. tblA has the following fields: ordernum and numberlabels tblB has the following fields: ordernum and labelnum What I need to do is this: For each record in tblA I need to create a number of new records in tblB based on the value of tblA.numberlabels. So for example: tblA ordernum numberlabels 123 5 I need to output to tblB the following: ordernum labelnum 123 1 123 2 123 3 123 4 123 5 If there are additional order numbers I need to loop through those as well. Hopefully you get the idea of what I need to do at this point. Here is the problem. I setup a form with a button on it and linked in to the OnClick event. I then placed the following code: Private Sub Command0_Click() Dim dbs As DAO.Database Dim rst As Recordset Dim rst2 As Recordset Dim x As Integer Dim i As Integer Set dbs = CurrentDb 'Set rst = dbs.OpenRecordset("tblA", dbPessimistic) 'rst.MoveFirst Set rst = dbs.OpenRecordset("tblB") 'While Not rst.EOF For i = 1 To 5 rst.AddNew rst!Fields("ordernum") = 1 rst!Fields("labelnum") = i rst.Update Next i 'Wend End Sub I have set Option Explicit under the General section. As you can see by my code I am only trying to update the table with hard-coded data at this point, not even trying to read from tblA. When I click on my command button to test this out, I get the following error: Run Time error '13' type mismatch When I click on Debug, this line is highlighted: Set rst = dbs.OpenRecordset("tblB") Can someone help me out? What am I missing? |
#3
| |||
| |||
|
|
I have seen a number of posts on the subject but I am unable to get them to work. I have been unable to find any information on the error that I am experiencing either. Here is the situation: I have two tables, let's say tblA and tblB. tblA has the following fields: ordernum and numberlabels tblB has the following fields: ordernum and labelnum What I need to do is this: For each record in tblA I need to create a number of new records in tblB based on the value of tblA.numberlabels. So for example: tblA ordernum numberlabels 123 5 I need to output to tblB the following: ordernum labelnum 123 1 123 2 123 3 123 4 123 5 If there are additional order numbers I need to loop through those as well. Hopefully you get the idea of what I need to do at this point. Here is the problem. I setup a form with a button on it and linked in to the OnClick event. I then placed the following code: Private Sub Command0_Click() Dim dbs As DAO.Database Dim rst As Recordset Dim rst2 As Recordset Dim x As Integer Dim i As Integer Set dbs = CurrentDb 'Set rst = dbs.OpenRecordset("tblA", dbPessimistic) 'rst.MoveFirst Set rst = dbs.OpenRecordset("tblB") 'While Not rst.EOF For i = 1 To 5 rst.AddNew rst!Fields("ordernum") = 1 rst!Fields("labelnum") = i rst.Update Next i 'Wend End Sub I have set Option Explicit under the General section. As you can see by my code I am only trying to update the table with hard-coded data at this point, not even trying to read from tblA. When I click on my command button to test this out, I get the following error: Run Time error '13' type mismatch When I click on Debug, this line is highlighted: Set rst = dbs.OpenRecordset("tblB") Can someone help me out? What am I missing? |
#4
| |||
| |||
|
|
Hi Julie, You're on the right track, try: Private Sub Command0_Click() Dim dbs As Database Dim rst As Recordset Dim rst2 As Recordset Dim i As Integer Set dbs = CurrentDb Set rst = dbs.OpenRecordset("tblA") Set rst2 = dbs.OpenRecordset("tblB") While Not rst.EOF For i = 1 To rst!numberlabels rst2.AddNew rst2!ordernum = rst!ordernum rst2!labelnum = i rst2.Update Next i Wend End Sub Regards, Peter... |
#5
| |||
| |||
|
#6
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |