473,471 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

excel to access in vb

codeCruncher
8 New Member
Hi! im a newbie here n im kinda stuck at dis function. This function is to import excel file(book1.xls) into a an access(97) table(name Service). I am using the code below but sumhow its not working. All i get is an error saying method 'range' of object '_global' failed. Can any1 help me detect the problem pleeeze...tq:)



Sub ConvertExcel()
Dim db As Database, rs As Recordset, r As Integer
Dim k

Set db = OpenDatabase("C:\RMIV\RMIV.mdb")
' open the database
Set rs = db.OpenRecordset("service", dbOpenTable)
' get all records in a table
r = 2 'skip 1; table header

k = ("A" & r)
Do While Len(Range(k).Formula) > 0

' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("TRNNO") = Range("A" & r).Value
.Fields("TRNDATE") = Range("B" & r).Value
.Fields("VECNO") = Range("C" & r).Value
.Fields("CMR") = Range("D" & r).Value
.Fields("NMR") = Range("E" & r).Value
.Fields("LOCATION") = Range("F" & r).Value
.Fields("DRIVER") = Range("G" & r).Value
.Fields("SP_CODE") = Range("H" & r).Value
.Fields("INVNO") = Range("I" & r).Value
.Fields("INVDATE") = Range("J" & r).Value
.Fields("INVAMT") = Range("K" & r).Value
.Fields("NARRATION") = Range("L" & r).Value
.Fields("MARK") = Range("M" & r).Value
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
MsgBox "Finish!", vbInformation, "Import"

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub
Sep 11 '07 #1
1 1260
r035198x
13,262 MVP
Hi! im a newbie here n im kinda stuck at dis function. This function is to import excel file(book1.xls) into a an access(97) table(name Service). I am using the code below but sumhow its not working. All i get is an error saying method 'range' of object '_global' failed. Can any1 help me detect the problem pleeeze...tq:)



Sub ConvertExcel()
Dim db As Database, rs As Recordset, r As Integer
Dim k

Set db = OpenDatabase("C:\RMIV\RMIV.mdb")
' open the database
Set rs = db.OpenRecordset("service", dbOpenTable)
' get all records in a table
r = 2 'skip 1; table header

k = ("A" & r)
Do While Len(Range(k).Formula) > 0

' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("TRNNO") = Range("A" & r).Value
.Fields("TRNDATE") = Range("B" & r).Value
.Fields("VECNO") = Range("C" & r).Value
.Fields("CMR") = Range("D" & r).Value
.Fields("NMR") = Range("E" & r).Value
.Fields("LOCATION") = Range("F" & r).Value
.Fields("DRIVER") = Range("G" & r).Value
.Fields("SP_CODE") = Range("H" & r).Value
.Fields("INVNO") = Range("I" & r).Value
.Fields("INVDATE") = Range("J" & r).Value
.Fields("INVAMT") = Range("K" & r).Value
.Fields("NARRATION") = Range("L" & r).Value
.Fields("MARK") = Range("M" & r).Value
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
MsgBox "Finish!", vbInformation, "Import"

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub
Moved to VB forum
Sep 11 '07 #2

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

Similar topics

8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
3
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown....
3
by: hari krishna | last post by:
hi, I am generating excel reports through vb.Net. After creating excel.application and the report is generated, each report leaves Excel in memory. I can see them in task manager in Process tab...
9
by: hari krishna | last post by:
hi, I want to send the data from dataset information to Excel through ASP.Net. there will be no XL installed on web server. web server is win 2000 server machine. I am using visual basic code in...
12
by: D. Shane Fowlkes | last post by:
This most likely belongs in another forum but I thought I'd start here. I have a COM Object written in VB6. The DLL will access MS Excel and use it's Object Library to write a customized report...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
0
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may...
3
by: rlntemp-gng | last post by:
RE: Access 2003/Excel 2003 Problem: After I close the Access application completely, I go out to the Task Manager and there is an Excel.exe object still sitting out there. My Access...
0
NeoPa
by: NeoPa | last post by:
Many of us have noticed that there are some very useful functions available to you when using Excel, but these same functions are not available, as standard, in Access. A particular issue I had...
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
1
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...
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
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...
0
muto222
php
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.