473,625 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get an increment of records in an Access table?

49 New Member
I am using two recordsets for inputting data into an access table. I have used two loops for each recordset to do so. My intent is to read data into the table from an Excelsheet. My first recordset selects all columns from the excelsheet and does what it is supposed to do. The second recordset selects only a handful of columns from the Excelsheet, but when using the second recordset I am only able to get the first row of data transferred into the table and not able to get all rows of data in. The second recordset starts from column number 30 and field number 27 in the table. The format of the first Recordset is very similar the only difference being that the 1st one uses cStartColumn1 as input in the function argument inside the for loop and the second one uses iCol which indicates the location of current column to which the cursor is pointing to. I will include the code for the second recordset here:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim rs2 As DAO.Recordset
  3.     Dim strSQL As String
  4.     Dim iColumn As Integer
  5.     Dim iField As Integer
  6.     Dim lrecords1 As Long
  7.     iCol = iCol + 2    'increments the column from 28 which is where the first recordset was stopped to 30 which is the starting column for the second.
  8.  
  9. strSQL = "SELECT [Field Measurement (m)],[Line Completed?], [Major Defects(codes only)],[CCTV Inspection Comments], [Date] FROM " & sTable
  10.     Set rs2 = dbs.OpenRecordset(strSQL)
  11.  
  12.  
  13.      With rs2
  14.     .AddNew
  15.      End With
  16.  
  17.  iRow = cStartRow1
  18.  iColumn = iCol
  19.  
  20.  Do While Not wks.Cells(iRow, 30) = ""
  21.  
  22.     cStartField1 = rs2.Fields(0)
  23.  
  24.     iField = cStartField1
  25.     lrecords1 = lrecords1 + 1    'this is the line where I am supposed to see the records incremented in the access table.
  26.  
  27.  For iColumn = iCol To iCol + (rs2.Fields.Count - (cStartField1 + 1))
  28.  
  29.          rs2.Fields(iField) = wks.Cells(iRow, iColumn)
  30.          DoCmd.Hourglass True
  31.          iField = iField + 1
  32.  
  33. If iField = 5 Then
  34.          rs2("Date").value = wks.Cells(3, 3)
  35.                  End If
  36.  
  37.       Next
  38.  
  39. iRow = iRow + 1
  40.       rs2.Update
  41.       rs2.AddNew
  42.  
  43.     loop
  44.  
  45.       rs2.Close
  46.       Set rs2 = Nothing
  47.  
  48.  
Feb 1 '11 #1
18 2803
ADezii
8,834 Recognized Expert Expert
The following would be the Logic for Appending Data from one Recordset to another with Fields being misaligned:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst_1 As DAO.Recordset
  3. Dim rst_2 As DAO.Recordset
  4.  
  5. Set MyDB = CurrentDb
  6. Set rst_1 = MyDB.OpenRecordset("tblTest", dbOpenForwardOnly)
  7. Set rst_2 = MyDB.OpenRecordset("tblAppend", dbOpenDynaset)
  8.  
  9. With rst_1
  10.   Do While Not .EOF
  11.     rst_2.AddNew
  12.       'Don't forget that the Fields Collection is Indexed starting at 0
  13.       rst_2.Fields(0) = .Fields(1)
  14.       rst_2.Fields(1) = .Fields(2)
  15.       rst_2.Fields(2) = .Fields(6)
  16.       rst_2.Fields(3) = .Fields(8)
  17.       rst_2.Fields(4) = .Fields(10)
  18.     rst_2.Update
  19.        .MoveNext
  20.   Loop
  21. End With
  22.  
  23. rst_1.Close
  24. rst_2.Close
  25. Set rst_1 = Nothing
  26. Set rst_2 = Nothing
Feb 1 '11 #2
Ehsan arman
49 New Member
Thanks ADzeii, but I am looking for is being able to merge two different recordsets from the same table into the same table. So, one recordset has all the fields in table A and the other selects only a few fields from table A. I am doing this because I am skipping two columns in excel so by opening rst_2 I am only selecting the columns that come after the skipped columns. The other problem is only seeing one row of data displayed in my table for rst_2
Feb 2 '11 #3
ADezii
8,834 Recognized Expert Expert
How about Posting some sample Data along with the desired results? In this manner, we can get a much clearer picture of exactly what you are requesting.
Feb 2 '11 #4
Ehsan arman
49 New Member
Hi, I have attached the original spreadsheet as For Me.xls
and I also copied what I have in my Access table into book1.xls and attached them both as a zip file.
So in there you'll see that my 2nd recordset fills up columns (Field Measurement through Date), but it's only one row of data.
Attached Files
File Type: zip Book1.zip (2.6 KB, 90 views)
File Type: zip For Me.zip (8.1 KB, 101 views)
Feb 2 '11 #5
ADezii
8,834 Recognized Expert Expert
Without the Code in its entirety, and not having the Database to look at, I simply posted what I feel the general Logic may be:
Expand|Select|Wrap|Line Numbers
  1. Dim rs2 As DAO.Recordset
  2. Dim strSQL As String
  3. Dim iColumn As Integer
  4. Dim iField As Integer
  5. Dim lrecords1 As Long
  6. iCol = iCol + 2    'increments the column from 28 which is where the first recordset was stopped to
  7.                    '30 which is the starting column for the second.
  8.  
  9. strSQL = "SELECT [Field Measurement (m)],[Line Completed?], [Major Defects(codes only)], " & _
  10.          "[CCTV Inspection Comments], [Date] FROM " & sTable
  11.  
  12. Set rs2 = dbs.OpenRecordset(strSQL)
  13.  
  14. iRow = cStartRow1
  15. iColumn = iCol
  16.  
  17. DoCmd.Hourglass True
  18.  
  19. Do While Not wks.Cells(iRow, 30) = ""
  20.   cStartField1 = rs2.Fields(0)
  21.  
  22.   iField = cStartField1
  23.   'Do you really need this?
  24.   lrecords1 = lrecords1 + 1    'this is the line where I am supposed to see the
  25.                                'records incremented in the access table.
  26.  
  27.     For iColumn = iCol To iCol + (rs2.Fields.Count - (cStartField1 + 1))
  28.       rs2.AddNew
  29.         rs2.Fields(iField) = wks.Cells(iRow, iColumn)
  30.           iField = iField + 1
  31.             If iField = 5 Then
  32.               rs2("Date").Value = wks.Cells(3, 3)
  33.             End If
  34.       rs2.Update
  35.     Next
  36.       iRow = iRow + 1
  37. Loop
  38.  
  39. DoCmd.Hourglass False
  40.  
  41. rs2.Close
  42. Set rs2 = Nothing
Feb 2 '11 #6
Ehsan arman
49 New Member
I tried that format but it didn't work. If you look in For Me.xls you can see that from the last 4 columns in there only the first Row of data(23.0, NO, qwe, asdasd) is found in the Access Table(book1.xls ) so for some reason the records dont get incremented in the table and the other 3 rows are lost. Also, how about merging the two recordsets rst and rs2? Will I be able to do that?
I will put in the rest of the code here just in case.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command20_Click()
  2.  
  3. msg = ProcessFileImport(sOutput, "CCTVPipeTemporary", 8, 2, 1, 1)
  4.  
  5. End Sub
  6.  
  7. Public Function ProcessFileImport(sFile As String, sTable As String, cStartRow1 As Byte, cStartColumn1 As Byte, cStartField1 As Byte, cTab1 As Integer) As String
  8.  
  9.  Dim wbk As New Excel.Workbook
  10.  
  11.    Dim ExcelApp As New Excel.Application
  12.    Dim ExcelBook As New Excel.Workbook
  13.    Dim wks As New Excel.Worksheet
  14.    Dim rngDefine As Excel.Range
  15.    Dim Rng As Excel.Range
  16.  
  17.    ' Access object variables
  18.    Dim dbs As DAO.Database
  19.    Dim rst As DAO.Recordset
  20.    Dim sSQL As String
  21.  
  22.  
  23.    Dim lrecords As Long
  24.    Dim iRow As Integer
  25.    Dim iCol As Integer
  26.    Dim iFld As Integer
  27.    Dim Message As String
  28.   ' Dim iShot As Integer
  29.    'Dim templ As String
  30.  
  31.   Set ExcelBook = ExcelApp.Workbooks.Open(sFile)
  32.   Set wbk = ExcelApp.Workbooks.Open(sFile)
  33.   Set wks = ExcelApp.Worksheets(cTab1)
  34.  
  35.  
  36. ' First Recordset
  37.  Set dbs = CurrentDb
  38.    sSQL = "SELECT * FROM " & sTable
  39.    Set rst = dbs.OpenRecordset(sSQL)
  40.  
  41.    With rst
  42.    .AddNew
  43.    End With
  44.  
  45.    iCol = cStartColumn1
  46.    iRow = cStartRow1
  47.  
  48. 'Stop
  49.    Do While Not wks.Cells(iRow, 1) = ""
  50.  
  51.    cStartField1 = rst.Fields(0)
  52.  
  53.       iFld = cStartField1
  54.       lrecords = lrecords + 1
  55.  
  56.       For iCol = cStartColumn1 To cStartColumn1 + (rst.Fields.Count - (cStartField1 + 1))
  57.  
  58.            If iCol = 28 Then
  59.               Exit For
  60.  
  61.             End If
  62.  
  63.          rst.Fields(iFld) = wks.Cells(iRow, iCol)
  64.          DoCmd.Hourglass True
  65.          iFld = iFld + 1
  66.  
  67.       Next
  68.  
  69.       'iCol = cStartColumn1
  70.       iRow = iRow + 1
  71.       rst.Update
  72.       rst.AddNew
  73.  
  74.  
  75.    Loop
  76.  
  77.   ' rst.Close
  78.   ' Set rst = Nothing
  79.  
  80.     Dim rs2 As DAO.Recordset
  81.     Dim strSQL As String
  82.     Dim iColumn As Integer
  83.     Dim iField As Integer
  84.     Dim lrecords1 As Long
  85.     iCol = iCol + 2
  86.     iFld = iFld + 3
  87.     iField = iFld
  88.  
  89.  
  90.  '2nd Recordset
  91.     strSQL = "SELECT [Field Measurement (m)],[Line Completed?], [Major Defects(codes only)],[CCTV Inspection Comments], [Date] FROM " & sTable
  92.     Set rs2 = dbs.OpenRecordset(strSQL)
  93.  
  94.      With rs2
  95.     .AddNew
  96.     End With
  97.  
  98.     'iColumn = cStartColumn1
  99.  
  100.  
  101.     iRow = cStartRow1
  102.    ' iField = cStartField1
  103.     iColumn = iCol
  104.   '  lrecords1 = iRow
  105.  
  106.  
  107.     Do While Not wks.Cells(iRow, 30) = ""
  108.  
  109.     cStartField1 = rs2.Fields(0)
  110.  
  111.     iField = cStartField1
  112.     lrecords1 = lrecords1 + 1
  113.  
  114.     For iColumn = iCol To iCol + (rs2.Fields.Count - (cStartField1 + 1))
  115.  
  116.          rs2.Fields(iField) = wks.Cells(iRow, iColumn)
  117.  
  118.              DoCmd.Hourglass True
  119.              iField = iField + 1
  120.  
  121.            '  lrecords = lrecords + 1
  122.  
  123.            If iField = 5 Then
  124.               rs2("Date").value = wks.Cells(3, 3)
  125.                lrecords = lrecords + 1
  126.            End If
  127.  
  128.            DoCmd.Hourglass False
  129.  
  130.       Next
  131.  
  132.       iRow = iRow + 1
  133.       rs2.Update
  134.       rs2.AddNew
  135.  
  136.    Loop
  137.  
  138.       rst.Close
  139.       Set rst = Nothing
  140.       rs2.Close
  141.       Set rs2 = Nothing
  142.  
  143.  
  144.  
  145.     Ltotal = DCount("*", "CCTV_Pipe_Daily_Report")
  146.     MsgBox ("Import Process is Done!" & vbclrf & vbCrLf & "There are" & Ltotal & " Records imported into the database")
  147.   ' ProcessFileImport = "Number of Pipes imported into the Database: " & lRecords
  148.  
  149. Exit_Here:
  150.    ' Cleanup all objects  (resume next on errors)
  151.    On Error Resume Next
  152.    Set wks = Nothing
  153.    Set wbk = Nothing
  154.    Set appExcel = Nothing
  155.    Set ExcelApp = Nothing
  156.    Set rst = Nothing
  157.    Set dbs = Nothing
  158.    DoCmd.Hourglass False
  159.    Exit Function
  160.  
  161. err_Handler:
  162.    ProcessFileImport = Err.Description
  163.    'Me.lblMsg.Caption = Err.Description
  164.    Resume Exit_Here
  165.  
  166. End Function
  167.  
  168.  
  169.  
Feb 2 '11 #7
ADezii
8,834 Recognized Expert Expert
Rather than restructure the Code, which I think it really needs, I created a radically different approach. I Imported the entire applicable Range (B8:AG27) into a Temporary Table, then Appended specific Fields into CCTVPipeTempora ry. Rather than go into specifics, download the Attachment and see if it can be of any use to you.

P.S. - Don't forget to change the Value of the Constant (conPATH) to point to your Excel Spreadsheet (For Me.xls).
Attached Files
File Type: zip Test.zip (21.2 KB, 99 views)
Feb 3 '11 #8
Ehsan arman
49 New Member
oh I can't open the file Test.zip. It says "the file is located outside your intranet or on an untrusted site"
Feb 3 '11 #9
ADezii
8,834 Recognized Expert Expert
Talk to your Network Administrator who should be able to fix the problem.
Feb 3 '11 #10

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

Similar topics

3
4306
by: Phil Rutter | last post by:
Hello All, I have about 700 word documents that have 2 tables one is static 4 colums x 5 rows the other is 5 colums x rows ranging from 2 to 100 what i wolud like to do is open the word doc. import the first word table then import the second word table close word doc open next word doc and repeat process. i am able to import one set of data currently into an excel spread sheet but how can i loop through all the documents and import them...
1
2500
by: Dave | last post by:
I have a form which is based on a table (a) with over 6000 records.I have a button on this form which will open another form with related data from another table (b). What I want to do is open the second form and see the related data or if it hasnt got any related data create a new record in table (b) so that I can input data. If possible I dont want to create a one to one relationship between the tables but create the new records in table...
0
435
by: VMI | last post by:
My windows app contains a process that loads data to a datatable. Is it possible to load this data into an Access table with exactly the same table structure? For example, if my process adds 100 rows to a datatable, I'd like to be able to dump the datatable into the Access table every 5 records, clear contents of the datatable, and then fill the datatable again. During the last "dump", the datatable would contain the last 5 records, while...
2
2028
by: VMI | last post by:
I have a datatable with emplcodes and I need to extract, from my Access table, all records that contain an emplcode included in my datatable. In other words, I need to do a query that accesses a datatable and an access table. How can this be done? I can't load the Access table into a datatable; it's too much data. Thanks.
12
7561
by: VMI | last post by:
For some reason, the process of retrieving data (about 20 records) from an Access table that has 400K records to a dataTable is taking over 3 mins. to complete. Below is my code to connect to the DB and query the table. The table "audit" primary key is "Line". Another weird thing (but I guess that's another post) is that, while it's doing the dataset Fill, my PC is slowed done substantially. But I don't know why that would happen since...
5
5087
by: Doomster | last post by:
I have Office 2000. I am creating a database that will record stats from football, basketball, and hockey games. One table will contain info about a game. The game could be a football, basketball, or hockey game. There will be at least 2700+ records per season. Each record will have more than 20 fields each. The table will have games from varous seasons so I can see it containing very many records (40000+). Is there a maximum size...
0
1264
by: NasirMunir | last post by:
I am trying to compare records from oracle table with a table in access. With oraDynaset, I have the option of .findNext function. I can use that to look for matching records from access to oracle. My problem is: I have to look up in access table and delete the records from oracle table if a certain record is not available in the access table. Is there a way of doing that ? Can I get some help ? To make my point more clear, here is the code ,...
15
16170
by: OfficeDummy | last post by:
Hello all! I searched the Internet and this forum for a similar problem, but I found no help... Also, I'm a complete newbie to the fascinating world of programming, VBA and Access, so my question can very well be very stupid. The tasks are: 1)Import an Excel table into Access 2)Add a new column and fill it with variables of date/time type. Steps 1 and 2 need to be done only once, and I've almost managed to accomplish them. Now...
12
8118
by: anand padia | last post by:
I have a master access table where we store all the employee information. I have various application developed in excel which imports and uses information in master. Now I want to develop a excel application that will import all the records from access table or some particular data (defined by field id) in excel spreadsheet. (***successfully acomplished) User would be able to view the records and make changes to the spreadsheet now when...
0
8251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8688
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8494
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7178
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.