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: -
-
Dim rs2 As DAO.Recordset
-
Dim strSQL As String
-
Dim iColumn As Integer
-
Dim iField As Integer
-
Dim lrecords1 As Long
-
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.
-
-
strSQL = "SELECT [Field Measurement (m)],[Line Completed?], [Major Defects(codes only)],[CCTV Inspection Comments], [Date] FROM " & sTable
-
Set rs2 = dbs.OpenRecordset(strSQL)
-
-
-
With rs2
-
.AddNew
-
End With
-
-
iRow = cStartRow1
-
iColumn = iCol
-
-
Do While Not wks.Cells(iRow, 30) = ""
-
-
cStartField1 = rs2.Fields(0)
-
-
iField = cStartField1
-
lrecords1 = lrecords1 + 1 'this is the line where I am supposed to see the records incremented in the access table.
-
-
For iColumn = iCol To iCol + (rs2.Fields.Count - (cStartField1 + 1))
-
-
rs2.Fields(iField) = wks.Cells(iRow, iColumn)
-
DoCmd.Hourglass True
-
iField = iField + 1
-
-
If iField = 5 Then
-
rs2("Date").value = wks.Cells(3, 3)
-
End If
-
-
Next
-
-
iRow = iRow + 1
-
rs2.Update
-
rs2.AddNew
-
-
loop
-
-
rs2.Close
-
Set rs2 = Nothing
-
-
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: - Dim MyDB As DAO.Database
-
Dim rst_1 As DAO.Recordset
-
Dim rst_2 As DAO.Recordset
-
-
Set MyDB = CurrentDb
-
Set rst_1 = MyDB.OpenRecordset("tblTest", dbOpenForwardOnly)
-
Set rst_2 = MyDB.OpenRecordset("tblAppend", dbOpenDynaset)
-
-
With rst_1
-
Do While Not .EOF
-
rst_2.AddNew
-
'Don't forget that the Fields Collection is Indexed starting at 0
-
rst_2.Fields(0) = .Fields(1)
-
rst_2.Fields(1) = .Fields(2)
-
rst_2.Fields(2) = .Fields(6)
-
rst_2.Fields(3) = .Fields(8)
-
rst_2.Fields(4) = .Fields(10)
-
rst_2.Update
-
.MoveNext
-
Loop
-
End With
-
-
rst_1.Close
-
rst_2.Close
-
Set rst_1 = Nothing
-
Set rst_2 = Nothing
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
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.
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.
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: - Dim rs2 As DAO.Recordset
-
Dim strSQL As String
-
Dim iColumn As Integer
-
Dim iField As Integer
-
Dim lrecords1 As Long
-
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.
-
-
strSQL = "SELECT [Field Measurement (m)],[Line Completed?], [Major Defects(codes only)], " & _
-
"[CCTV Inspection Comments], [Date] FROM " & sTable
-
-
Set rs2 = dbs.OpenRecordset(strSQL)
-
-
iRow = cStartRow1
-
iColumn = iCol
-
-
DoCmd.Hourglass True
-
-
Do While Not wks.Cells(iRow, 30) = ""
-
cStartField1 = rs2.Fields(0)
-
-
iField = cStartField1
-
'Do you really need this?
-
lrecords1 = lrecords1 + 1 'this is the line where I am supposed to see the
-
'records incremented in the access table.
-
-
For iColumn = iCol To iCol + (rs2.Fields.Count - (cStartField1 + 1))
-
rs2.AddNew
-
rs2.Fields(iField) = wks.Cells(iRow, iColumn)
-
iField = iField + 1
-
If iField = 5 Then
-
rs2("Date").Value = wks.Cells(3, 3)
-
End If
-
rs2.Update
-
Next
-
iRow = iRow + 1
-
Loop
-
-
DoCmd.Hourglass False
-
-
rs2.Close
-
Set rs2 = Nothing
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. -
Private Sub Command20_Click()
-
-
msg = ProcessFileImport(sOutput, "CCTVPipeTemporary", 8, 2, 1, 1)
-
-
End Sub
-
-
Public Function ProcessFileImport(sFile As String, sTable As String, cStartRow1 As Byte, cStartColumn1 As Byte, cStartField1 As Byte, cTab1 As Integer) As String
-
-
Dim wbk As New Excel.Workbook
-
-
Dim ExcelApp As New Excel.Application
-
Dim ExcelBook As New Excel.Workbook
-
Dim wks As New Excel.Worksheet
-
Dim rngDefine As Excel.Range
-
Dim Rng As Excel.Range
-
-
' Access object variables
-
Dim dbs As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim sSQL As String
-
-
-
Dim lrecords As Long
-
Dim iRow As Integer
-
Dim iCol As Integer
-
Dim iFld As Integer
-
Dim Message As String
-
' Dim iShot As Integer
-
'Dim templ As String
-
-
Set ExcelBook = ExcelApp.Workbooks.Open(sFile)
-
Set wbk = ExcelApp.Workbooks.Open(sFile)
-
Set wks = ExcelApp.Worksheets(cTab1)
-
-
-
' First Recordset
-
Set dbs = CurrentDb
-
sSQL = "SELECT * FROM " & sTable
-
Set rst = dbs.OpenRecordset(sSQL)
-
-
With rst
-
.AddNew
-
End With
-
-
iCol = cStartColumn1
-
iRow = cStartRow1
-
-
'Stop
-
Do While Not wks.Cells(iRow, 1) = ""
-
-
cStartField1 = rst.Fields(0)
-
-
iFld = cStartField1
-
lrecords = lrecords + 1
-
-
For iCol = cStartColumn1 To cStartColumn1 + (rst.Fields.Count - (cStartField1 + 1))
-
-
If iCol = 28 Then
-
Exit For
-
-
End If
-
-
rst.Fields(iFld) = wks.Cells(iRow, iCol)
-
DoCmd.Hourglass True
-
iFld = iFld + 1
-
-
Next
-
-
'iCol = cStartColumn1
-
iRow = iRow + 1
-
rst.Update
-
rst.AddNew
-
-
-
Loop
-
-
' rst.Close
-
' Set rst = Nothing
-
-
Dim rs2 As DAO.Recordset
-
Dim strSQL As String
-
Dim iColumn As Integer
-
Dim iField As Integer
-
Dim lrecords1 As Long
-
iCol = iCol + 2
-
iFld = iFld + 3
-
iField = iFld
-
-
-
'2nd Recordset
-
strSQL = "SELECT [Field Measurement (m)],[Line Completed?], [Major Defects(codes only)],[CCTV Inspection Comments], [Date] FROM " & sTable
-
Set rs2 = dbs.OpenRecordset(strSQL)
-
-
With rs2
-
.AddNew
-
End With
-
-
'iColumn = cStartColumn1
-
-
-
iRow = cStartRow1
-
' iField = cStartField1
-
iColumn = iCol
-
' lrecords1 = iRow
-
-
-
Do While Not wks.Cells(iRow, 30) = ""
-
-
cStartField1 = rs2.Fields(0)
-
-
iField = cStartField1
-
lrecords1 = lrecords1 + 1
-
-
For iColumn = iCol To iCol + (rs2.Fields.Count - (cStartField1 + 1))
-
-
rs2.Fields(iField) = wks.Cells(iRow, iColumn)
-
-
DoCmd.Hourglass True
-
iField = iField + 1
-
-
' lrecords = lrecords + 1
-
-
If iField = 5 Then
-
rs2("Date").value = wks.Cells(3, 3)
-
lrecords = lrecords + 1
-
End If
-
-
DoCmd.Hourglass False
-
-
Next
-
-
iRow = iRow + 1
-
rs2.Update
-
rs2.AddNew
-
-
Loop
-
-
rst.Close
-
Set rst = Nothing
-
rs2.Close
-
Set rs2 = Nothing
-
-
-
-
Ltotal = DCount("*", "CCTV_Pipe_Daily_Report")
-
MsgBox ("Import Process is Done!" & vbclrf & vbCrLf & "There are" & Ltotal & " Records imported into the database")
-
' ProcessFileImport = "Number of Pipes imported into the Database: " & lRecords
-
-
Exit_Here:
-
' Cleanup all objects (resume next on errors)
-
On Error Resume Next
-
Set wks = Nothing
-
Set wbk = Nothing
-
Set appExcel = Nothing
-
Set ExcelApp = Nothing
-
Set rst = Nothing
-
Set dbs = Nothing
-
DoCmd.Hourglass False
-
Exit Function
-
-
err_Handler:
-
ProcessFileImport = Err.Description
-
'Me.lblMsg.Caption = Err.Description
-
Resume Exit_Here
-
-
End Function
-
-
-
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).
oh I can't open the file Test.zip. It says "the file is located outside your intranet or on an untrusted site"
ADezii 8,834
Recognized Expert Expert
Talk to your Network Administrator who should be able to fix the problem.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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.
|
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...
| |
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...
|
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 ,...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |