473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Error: Either BOF or EOF is True, or the current record...

doma23
107 100+
Hi, I have 3 tables and 1 master table:
tblInfo1, tblInfo2, tblInfo3, tblMaster.
All tables have the same fields.
Two of them are [code] and [date]. This is unique combination.

I need to open and compare in VBA all three tables with Master table. If the data already exist in tblMaster, meaning [code] and [date] are the same, I just need to overwrite the record in tblMaster. If the data are new, I need to add a new record.

Thank you.

----------
P.S. I was trying something on my own, I've opened connection to 2 databases and I've tried to compare the records, but I don't know the syntax.
My logic was that it should be something like this:

rs.Open "tblInfo1"
rs.Open "tblMaster"

Do While "tblInfo1" EOF
rs.MoveFirst
If rs![code] = rs(tblMaster)![code] and rs![date] = rs(tblMaster)![date] Then
rs(tblMaster)![data1] = rs![data1]
rs(tblMaster)![data2] = rs![data2]
rs.Update
Else
rs.AddNew
rs(tblMaster)![data1] = rs![data1]
rs(tblMaster)![data2] = rs![data2]
rs.Update
End If
Loop

This is just an idea of the code logic.
Oct 1 '10 #1

✓ answered by ADezii

Simply pass to the Function the Name of the Table that you wish to compare to tblMaster, and the Code should do the rest. I'll post the Code as well as the sample Calls, but I strongly suggest that you download the Attachment that I have made available to you, in order to get a much clearer picture of the Logic involved. All Fields are TEXT except [Date], [series_code] and [Date] comprise a 'Unique' Index, and the same Fields are consistent in Name and Data Types across all Tables. Modify the Values in tblInfo1, then see what happens to tblMaster. Have fun, and should you have any questions, feel free to ask.
  1. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCompareData(strTableName As String)
    2. Dim rstCompare As ADODB.Recordset
    3. Dim strADD As String
    4. Dim strUpd As String
    5.  
    6. Set rstCompare = New ADODB.Recordset
    7.  
    8. DoCmd.Hourglass True
    9.  
    10. 'Kept distinct for the sake of clarity
    11. With rstCompare
    12.   .Source = strTableName
    13.   .ActiveConnection = CurrentProject.Connection
    14.   .CursorType = adOpenDynamic
    15.   .LockType = adLockOptimistic
    16.     .Open
    17. End With
    18.  
    19. 'Let's test every Record in strTableName, and see if the Unique Combination of [Date] and [series_code]
    20. 'exists in tblMaster. If a Match exists, overwrite [sheet_name] and [Value] in the Master
    21. 'Table with the Values in the Recordset. Should no Match exist, then Add the New Record to tblMaster.
    22. With rstCompare
    23.   Do While Not .EOF
    24.     If DCount("*", "tblMaster", "[series_code] = '" & ![series_code] & "' AND [Date] = #" & ![Date] & "#") > 0 Then
    25.       strUpd = "UPDATE tblMaster Set [sheet_name] = '" & ![sheet_name] & "', [Value] = '" & ![Value] & "' WHERE " & _
    26.                "[Date] = #" & ![Date] & "# AND [series_code] = '" & ![series_code] & "';"
    27.                   CurrentDb.Execute strUpd, dbFailOnError
    28.                     Debug.Print "UPDATE ==> " & ![Date] & " | " & ![series_code]
    29.     Else        '[series_code] <==> [Date] combination doesn't exist, so Add a New Record
    30.       strADD = "INSERT INTO tblMaster ([sheet_name], [Value], [Date], [series_code]) VALUES ('" & _
    31.                ![sheet_name] & "', '" & ![Value] & "', #" & ![Date] & "#, '" & ![series_code] & "')"
    32.                   CurrentDb.Execute strADD, dbFailOnError
    33.                     Debug.Print "ADD ==> " & ![Date] & " | " & ![series_code]
    34.     End If
    35.       .MoveNext
    36.   Loop
    37. End With
    38.  
    39. DoCmd.Hourglass False
    40.  
    41. rstCompare.Close
    42. Set rstCompare = Nothing
    43. End Function
    44.  
  2. Sample Call(s) to the Function:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdTest_Click()
    2. On Error GoTo Err_cmdTest_Click
    3.  
    4. Call fCompareData("tblInfo1")
    5.             'OR
    6. Call fCompareData("tblInfo2")
    7.             'OR
    8. Call fCompareData("tblInfo3")
    9.  
    10. Exit_cmdTest_Click:
    11.   Exit Sub
    12.  
    13. Err_cmdTest_Click:
    14.   MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
    15.     DoCmd.Hourglass False
    16.       Resume Exit_cmdTest_Click
    17. End Sub
  3. Download the Attachment below.

7 5546
doma23
107 100+
This down is the code I come up with.
However, now I'm receiving next error message:
"Either BOF or EOF is True, or the current record has been deleted."
When I go to debug it highlights the line 33:
"rsMaster![sheet_name] = rsInfo1![sheet_name]"

Expand|Select|Wrap|Line Numbers
  1. Private Sub Comando2_Click()
  2.  
  3. Dim cn As ADODB.Connection
  4.  
  5. Dim rsMaster As New ADODB.Recordset
  6. Dim rsInfo1 As New ADODB.Recordset
  7.  
  8.  
  9. Dim sCriteria As String
  10.  
  11. Set cn = CurrentProject.Connection
  12. Set rsMaster = New ADODB.Recordset
  13. Set rsInfo1 = New ADODB.Recordset
  14.  
  15. rsMaster.Open "tblMaster", cn, adOpenDynamic, adLockOptimistic
  16. rsInfo1.Open "tblInfo1", cn, adOpenDynamic, adLockOptimistic
  17.  
  18. rsMaster.MoveFirst
  19.  
  20. rsInfo1.MoveFirst
  21.  
  22. If Not rsInfo1.EOF Then
  23.  
  24.     Do While Not rsInfo1.EOF
  25.         'check if it exist in the rsMaster
  26.         'if exist then rs.filter
  27.  
  28.         If DCount("*", "[tblMaster]", _
  29.             "(([series_code]='" & rsInfo1![series_code] & "') AND (" & "[date]=#" & Format(rsInfo1![Date], "mm dd yyyy") & "#))") > 0 Then
  30.  
  31.             sCriteria = "(([series_code]='" & rsInfo1![series_code] & "') AND (" & "[date]=#" & Format(rsInfo1![Date], "mm dd yyyy") & "#))"
  32.             rsMaster.Filter = sCriteria
  33.             rsMaster![sheet_name] = rsInfo1![sheet_name]
  34.             rsMaster![Value] = rsInfo1![Value]
  35.             rsMaster.Update
  36.         Else
  37.             rsMaster.AddNew
  38.             rsMaster![series_code] = rsInfo1![series_code]
  39.             rsMaster![sheet_name] = rsInfo1![sheet_name]
  40.             rsMaster![Date] = rsInfo1![Date]
  41.             rsMaster![Value] = rsInfo1![Value]
  42.             rsMaster.Update
  43.         End If
  44.  
  45.     rsInfo1.MoveNext
  46.     Loop
  47.  
  48. Else
  49.     MsgBox ("No record found!")
  50. End If
  51.  
  52.  
  53. rsMaster.Close
  54. rsInfo1.Close
  55. cn.Close
  56. Set rsMaster = Nothing
  57. Set cn = Nothing
  58.  
  59. MsgBox "Data have been saved!", vbOKOnly
  60.  
  61. End Sub
Oct 1 '10 #2
Mariostg
332 100+
You must test for both EOF and BOF. So on line 24 have:
Do While Not rsInfo1.EOF And Not rsInfo1.BOF

And you can get rid of the IF clause on line 22.
Oct 1 '10 #3
doma23
107 100+
Unfortunately, that doesn't solve the problem. :(
Still showing run-time error 3021:
"Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record."

Actually in the beginning I have one dummy record in tblMaster, and after I click on the button for the first time it works good, and it adds new records. It copies all the records from tblInfo. But then, when I click again the second time it brings up this message.

The problem is for sure in line 33, in the "rsMaster![sheet_name] = ".
This code is the cause for this message.
Oct 4 '10 #4
ADezii
8,834 Expert 8TB
Just subscribing for now, will return later.
Oct 4 '10 #5
Mariostg
332 100+
Yes more that likely. In that case you filter may have something to do with it. Can you run your filter with the appropriate criteria by itself and see what is left in the rsMaster table. There might be no record left...
Oct 4 '10 #6
ADezii
8,834 Expert 8TB
I should have a solution shortly using a slightly different approach, namely: an ADODB Recordset in combination with Dynamic SQL Statement Execution, thereby eliminating the Recordset Filter.
Oct 4 '10 #7
ADezii
8,834 Expert 8TB
Simply pass to the Function the Name of the Table that you wish to compare to tblMaster, and the Code should do the rest. I'll post the Code as well as the sample Calls, but I strongly suggest that you download the Attachment that I have made available to you, in order to get a much clearer picture of the Logic involved. All Fields are TEXT except [Date], [series_code] and [Date] comprise a 'Unique' Index, and the same Fields are consistent in Name and Data Types across all Tables. Modify the Values in tblInfo1, then see what happens to tblMaster. Have fun, and should you have any questions, feel free to ask.
  1. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCompareData(strTableName As String)
    2. Dim rstCompare As ADODB.Recordset
    3. Dim strADD As String
    4. Dim strUpd As String
    5.  
    6. Set rstCompare = New ADODB.Recordset
    7.  
    8. DoCmd.Hourglass True
    9.  
    10. 'Kept distinct for the sake of clarity
    11. With rstCompare
    12.   .Source = strTableName
    13.   .ActiveConnection = CurrentProject.Connection
    14.   .CursorType = adOpenDynamic
    15.   .LockType = adLockOptimistic
    16.     .Open
    17. End With
    18.  
    19. 'Let's test every Record in strTableName, and see if the Unique Combination of [Date] and [series_code]
    20. 'exists in tblMaster. If a Match exists, overwrite [sheet_name] and [Value] in the Master
    21. 'Table with the Values in the Recordset. Should no Match exist, then Add the New Record to tblMaster.
    22. With rstCompare
    23.   Do While Not .EOF
    24.     If DCount("*", "tblMaster", "[series_code] = '" & ![series_code] & "' AND [Date] = #" & ![Date] & "#") > 0 Then
    25.       strUpd = "UPDATE tblMaster Set [sheet_name] = '" & ![sheet_name] & "', [Value] = '" & ![Value] & "' WHERE " & _
    26.                "[Date] = #" & ![Date] & "# AND [series_code] = '" & ![series_code] & "';"
    27.                   CurrentDb.Execute strUpd, dbFailOnError
    28.                     Debug.Print "UPDATE ==> " & ![Date] & " | " & ![series_code]
    29.     Else        '[series_code] <==> [Date] combination doesn't exist, so Add a New Record
    30.       strADD = "INSERT INTO tblMaster ([sheet_name], [Value], [Date], [series_code]) VALUES ('" & _
    31.                ![sheet_name] & "', '" & ![Value] & "', #" & ![Date] & "#, '" & ![series_code] & "')"
    32.                   CurrentDb.Execute strADD, dbFailOnError
    33.                     Debug.Print "ADD ==> " & ![Date] & " | " & ![series_code]
    34.     End If
    35.       .MoveNext
    36.   Loop
    37. End With
    38.  
    39. DoCmd.Hourglass False
    40.  
    41. rstCompare.Close
    42. Set rstCompare = Nothing
    43. End Function
    44.  
  2. Sample Call(s) to the Function:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdTest_Click()
    2. On Error GoTo Err_cmdTest_Click
    3.  
    4. Call fCompareData("tblInfo1")
    5.             'OR
    6. Call fCompareData("tblInfo2")
    7.             'OR
    8. Call fCompareData("tblInfo3")
    9.  
    10. Exit_cmdTest_Click:
    11.   Exit Sub
    12.  
    13. Err_cmdTest_Click:
    14.   MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
    15.     DoCmd.Hourglass False
    16.       Resume Exit_cmdTest_Click
    17. End Sub
  3. Download the Attachment below.
Attached Files
File Type: zip Compare.zip (19.0 KB, 121 views)
Oct 4 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dan Jones | last post by:
I have a database of books that was originally created as a flat file. Each record has a number of fields, including the authors name. I'm trying to convert the database to something a little more...
1
by: Alex Hemingway | last post by:
I have a table Products consisting of the following fields :- ProductID, ProductName, PriceLevel, UnitPrice and a query which returns all products with a specific PriceLevel. What I would like...
6
by: cyoung311 | last post by:
I'm trying to do an automatic update of one table from another through a form. Basically, when a selection is made for an item, in this case a particular workshop, I want to get the associated...
0
by: Leonardo Gangemi | last post by:
How to align right a table based on another table created dinamically? Leonardo
5
by: das | last post by:
hello all, this might be simple: I populate a temp table based on a condition from another table: select @condition = condition from table1 where id=1 in my stored procedure I want to do...
0
by: Dan | last post by:
I have an access database that contains a linked dbf table that is based on a shapefile in ARC-Editor. I would like to auto-update a table based on the new records that have been entered into GIS....
4
by: sql4life | last post by:
Hello Folks, I am attempting to update a single field in one table based on a select from two tables. However, I am receiving the following error. Any ideas are appreciated. ORA-00933: SQL...
5
by: koutoo | last post by:
If I wanted to accomplish looking up values from one table based on a value from another table or file, how would I go about doing this in Python? Would I build a dictionary or an array? exp: ...
2
by: tomash | last post by:
Hi! I ve got two tables in Access 2007. I want to update a field of DataTable from another table, DataSumTable when two of their fields equals. ( the fields : Name and Period) I tried this...
2
by: MrCrunchy | last post by:
Hi All I have 2 tables (as shown below), if i add a row to the FILES TABLE then the myid value is added to mydata list in the PLAYLISTS TABLE, based on the genre matching the myname values. Hope...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.