473,809 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code to append from excel


Hi---I have to import some data from excel into an Access table--the
code below is working ok but I would like to condense it by running a
loop. The number of records can vary from 1 to 30 in each excel
file--have tried to run a loop through the recs and it quits so I have
just repeated the code below 30 times--any suggestions on how to run a
loop instead and have it quit when it reaches the last line of the
excel file? This code is in an Excel macro

thanks
bob stafford
Sub AppendToAccess( )

Dim TheDB As Database
Dim TheRecordset As Recordset

'Sets Access Image Database
Set TheDB = OpenDatabase("C :\ImageTest.mdb ")
Set TheRecordset = TheDB.TableDefs ("SkinImagedFie lds"). _
OpenRecordset(d bOpenDynaset)
With TheRecordset

'Checks for Nulls; If null, then Exits Sub. If not, then Appends
Record

If Range("A1") = "" Then
Exit Sub

Else
.AddNew
.Fields("LabNum ") = Range("A1").Val ue
.Fields("Biopsy Num") = Range("B1").Val ue
.Fields("FieldN um") = Range("C1").Val ue
.Fields("Positi veNuclei") = Range("D1").Val ue
.Fields("Negati veNuclei") = Range("E1").Val ue
.Fields("NonNuc learArea") = Range("F1").Val ue
.Update
End If

If Range("A2") = "" Then
MsgBox "You Have Imported 1 Record", vbOKOnly, "Added Record
to Access"
Exit Sub
Else
.AddNew
.Fields("LabNum ") = Range("A2").Val ue
.Fields("Biopsy Num") = Range("B2").Val ue
.Fields("FieldN um") = Range("C2").Val ue
.Fields("Positi veNuclei") = Range("D2").Val ue
.Fields("Negati veNuclei") = Range("E2").Val ue
.Fields("NonNuc learArea") = Range("F2").Val ue
.Update
End If
If Range("A3") = "" Then
MsgBox "You Have Imported 2 Record", vbOKOnly, "Added Record
to Access"
Exit Sub
Else
.AddNew
.Fields("LabNum ") = Range("A3").Val ue
.Fields("Biopsy Num") = Range("B3").Val ue
.Fields("FieldN um") = Range("C3").Val ue
.Fields("Positi veNuclei") = Range("D3").Val ue
.Fields("Negati veNuclei") = Range("E3").Val ue
.Fields("NonNuc learArea") = Range("F3").Val ue
.Update
End If
repeated many times
Nov 13 '05 #1
6 4456
Ally:

Try this instead. It allows the user to specify how many rows to
Append, but will exit out if it hits a blank field in column A before
it reaches that number. If you want to, you can just declare a value
for MyLastCell in your code that is always larger than the number of
rows that would be used in your spreadsheet. This would mean you can
take out the lines referenced under the section that gets the user
input and replace it with the following:
MyLastCell = CInt(30)

Otherwise, it should work as is (fixing any lines that wrapped when I
pasted in the post).

Sub AppendToAccess( )

Dim TheDB As Database
Dim TheRecordset As Recordset
Dim MyCurrentCell, MyLastCell
Dim MyCount As Integer
Dim MyMsg As String

'Get number of rows from user--if no input, do nothing
MyLastCell = InputBox("What is the last row number to append?", "Enter
Number of Cells")
If MyLastCell = "" Then Exit Sub
MyLastCell = CInt(MyLastCell )

'Set cell references (Row #, Column #)
Set MyCurrentCell = ActiveSheet.Cel ls(1, 1) 'A1
Set MyLastCell = ActiveSheet.Cel ls(MyLastCell, 6) 'F30, if user
enters 30
X = 0

'Sets Access Image Database
Set TheDB = OpenDatabase("C :\ImageTest.mdb ")
Set TheRecordset = TheDB.TableDefs ("SkinImagedFie lds"). _
OpenRecordset(d bOpenDynaset)
With TheRecordset

'Checks for Nulls; If null, then Exits Loop. If not, then Appends
Record
Do Until MyCurrentCell.R ow > MyLastCell.Row
If MyCurrentCell.V alue = "" Then
Exit Do
Else
.AddNew
.Fields("LabNum ") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column B
.Fields("Biopsy Num") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column C
.Fields("FieldN um") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column D
.Fields("Positi veNuclei") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column E
.Fields("Negati veNuclei") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column F
.Fields("NonNuc learArea") = MyCurrentCell.V alue
.Update
End If
X = X + 1 'counter for number of records appended to Access
Set MyCurrentCell = ActiveSheet.Cel ls(X + 1, 1) 'Move to first
cell in next column
Loop
End With

'Create message for MsgBox
Select Case X
Case Is <= 0
MyMsg = "No Records Were Appended."
Case Is = 1
MyMsg = "You Have Imported 1 Record."
Case Else
MyMsg = "You Have Imported " & X & " Records."
End Select

'Display MsgBox
MsgBox MyMsg, vbOKOnly, "Append Results"
End Sub
I tested it beforehand and seems to do what you're requesting. Good
luck!

Jana

Nov 13 '05 #2
Bob:

Try this instead. It allows the user to input the desired number of
rows to append into Access for you, but will exit out as soon as it
reaches an empty cell in column A, even if it hasn't reached that
number yet. If you would prefer, you can replace the section that gets
the user input with a statement that just specifies a max number of
rows to iterate through. Just replace that section with the following
line:
MyLastCell = CInt(30)

This would set the maximum number of rows to 30. You can go a little
larger since it will stop as soon as it hits a blank cell in column A
anyway.

Here is the code, you'll probably have to clean up any word wrapping
that happened when I posted, but I tested it with a database and it
worked fine.

Sub AppendToAccess( )

Dim TheDB As Database
Dim TheRecordset As Recordset
Dim MyCurrentCell, MyLastCell
Dim X As Integer
Dim MyMsg As String

'Get number of rows from user--if no input, do nothing
MyLastCell = InputBox("What is the last row number to append?", "Enter
Number of Cells")
If MyLastCell = "" Then Exit Sub
MyLastCell = CInt(MyLastCell )

'Set cell references (Row #, Column #)
Set MyCurrentCell = ActiveSheet.Cel ls(1, 1) 'A1
Set MyLastCell = ActiveSheet.Cel ls(MyLastCell, 6) 'F30, if user
enters 30
X = 0
'Sets Access Image Database
Set TheDB = OpenDatabase("C :\ImageTest.mdb ")
Set TheRecordset = TheDB.TableDefs ("SkinImagedFie lds"). _
OpenRecordset(d bOpenDynaset)
With TheRecordset

'Checks for Nulls; If null, then Exits Loop. If not, then Appends
Record
Do Until MyCurrentCell.R ow > MyLastCell.Row
If MyCurrentCell.V alue = "" Then
Exit Do
Else
.AddNew
.Fields("LabNum ") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column B
.Fields("Biopsy Num") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column C
.Fields("FieldN um") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column D
.Fields("Positi veNuclei") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column E
.Fields("Negati veNuclei") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column F
.Fields("NonNuc learArea") = MyCurrentCell.V alue
.Update
End If
X = X + 1 'counter for number of records appended to Access
Set MyCurrentCell = ActiveSheet.Cel ls(X + 1, 1) 'Move to first
cell in next column
Loop
End With

'Create message for MsgBox
Select Case X
Case Is <= 0
MyMsg = "No Records Were Appended."
Case Is = 1
MyMsg = "You Have Imported 1 Record."
Case Else
MyMsg = "You Have Imported " & X & " Records."
End Select

'Display MsgBox
MsgBox MyMsg, vbOKOnly, "Append Results"
End Sub

Good luck!
Jana

Nov 13 '05 #3
Thanks--will try it and let you know how it worked out

Bob
On 24 Feb 2005 17:37:25 -0800, "Jana" <Ba********@gma il.com> wrote:
Ally:

Try this instead. It allows the user to specify how many rows to
Append, but will exit out if it hits a blank field in column A before
it reaches that number. If you want to, you can just declare a value
for MyLastCell in your code that is always larger than the number of
rows that would be used in your spreadsheet. This would mean you can
take out the lines referenced under the section that gets the user
input and replace it with the following:
MyLastCell = CInt(30)

Otherwise, it should work as is (fixing any lines that wrapped when I
pasted in the post).

Sub AppendToAccess( )

Dim TheDB As Database
Dim TheRecordset As Recordset
Dim MyCurrentCell, MyLastCell
Dim MyCount As Integer
Dim MyMsg As String

'Get number of rows from user--if no input, do nothing
MyLastCell = InputBox("What is the last row number to append?", "Enter
Number of Cells")
If MyLastCell = "" Then Exit Sub
MyLastCell = CInt(MyLastCell )

'Set cell references (Row #, Column #)
Set MyCurrentCell = ActiveSheet.Cel ls(1, 1) 'A1
Set MyLastCell = ActiveSheet.Cel ls(MyLastCell, 6) 'F30, if user
enters 30
X = 0

'Sets Access Image Database
Set TheDB = OpenDatabase("C :\ImageTest.mdb ")
Set TheRecordset = TheDB.TableDefs ("SkinImagedFie lds"). _
OpenRecordset(d bOpenDynaset)
With TheRecordset

'Checks for Nulls; If null, then Exits Loop. If not, then Appends
Record
Do Until MyCurrentCell.R ow > MyLastCell.Row
If MyCurrentCell.V alue = "" Then
Exit Do
Else
.AddNew
.Fields("LabNum ") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column B
.Fields("Biopsy Num") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column C
.Fields("FieldN um") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column D
.Fields("Positi veNuclei") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column E
.Fields("Negati veNuclei") = MyCurrentCell.V alue
Set MyCurrentCell = MyCurrentCell.O ffset(0, 1) 'Move to
column F
.Fields("NonNuc learArea") = MyCurrentCell.V alue
.Update
End If
X = X + 1 'counter for number of records appended to Access
Set MyCurrentCell = ActiveSheet.Cel ls(X + 1, 1) 'Move to first
cell in next column
Loop
End With

'Create message for MsgBox
Select Case X
Case Is <= 0
MyMsg = "No Records Were Appended."
Case Is = 1
MyMsg = "You Have Imported 1 Record."
Case Else
MyMsg = "You Have Imported " & X & " Records."
End Select

'Display MsgBox
MsgBox MyMsg, vbOKOnly, "Append Results"
End Sub
I tested it beforehand and seems to do what you're requesting. Good
luck!

Jana


Nov 13 '05 #4
Bob:

How did that work out for you?

Just curious,
Jana

Nov 13 '05 #5
On 9 Mar 2005 15:10:02 -0800, "Jana" <Ba********@gma il.com> wrote:
Bob:

How did that work out for you?

Just curious,
Jana

Hi--sorry to not get back sooner--it worked exactly as we needed it!!
Thanks very much! Just cannot seem to get my head around looping :(
Nov 13 '05 #6
Yay! Glad it worked out for you :)

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
25062
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. eg. this only transferes the first record in the area. ..Fields("Uge").Value = ws.Range("A98").Value Sub SelectMaster()
2
2471
by: Greg Strong | last post by:
Hello All, I've written code in a test database with test data. Everything seems to be working except compact database in VB code per http://www.mvps.org/access/general/gen0041.htm. The reason I say this is the auto number fields are NOT being reset to zero. I delete the data from tables with action delete queries, then call the compact DB code which is followed by importing data to tables and subsequent append queries to other tables....
3
11095
by: Antoon Vanderplancke | last post by:
I want to link several worksheets of a workbook as linked tables. This is the vba-code I use, which don't work Any suggestions are welcome sub LinkExcel(strWorkBook) Dim appXLS as Excel.Application Dim wrk as Excel.Workbook Dim sht as Excel.WorkSheet
5
1786
by: rdemyan via AccessMonster.com | last post by:
I have a table with about 80 fields. I'm using an import process to populate the table. It works fine, except for the following: Users generally don't specify values for a lot of numerical fields on the spreadsheet. I set the Access table up so that all the numerical values would have a default value of zero. I thought that when the record was appended to the table that the numerical fields that do not have values would be...
1
1856
by: eighthman11 | last post by:
Hello everyone. I have a table linked to an excel spreadsheet. I have an append query based on this link table. I receive an error when I run the append query "numeric field overflow". I have noticed that when I open then save the append query and then run it I get no error. I am assuming that the error is being caused by Access Optimizing. The easy solution to my problem is if it is possible to run some VB code that will save my...
4
2048
by: ALaurie10 | last post by:
I am somewhat familar with access, but have no clue to modules and coding. I am trying to develop code or a module that will enable a user to import a specific excel spreadsheet and its' data into a table by pressing a command button. I am able to import excel, however it will not attach to existing spreadsheet. I find myself, having to build an append query for each import which is time consuming and inefficient. I have already checked, my...
1
2182
by: thanawala27 | last post by:
Hi, I wanted to append a cell of an Excel file. Basically, the excel file is not taking more than 256 characters for a cell at a time. So i thought of breaking the varaible into 2. n copying it one by one. But hte 2nd variable needs to be appended to the first variable in the Cell. I had posted something on related topic sometime back (http://www.thescripts.com/forum/thread682464.html), and i was able to find out that there is some...
3
1926
by: bazdaa | last post by:
Access 2K3 Append/Update ignore non null cells! Hi, I have been tasked with completing an audit of approximately 10,000 items, to which I have generated a list of 40 questions (fields) for each record. I began collating the information in Excel, but found myself getting lost and even experienced major data loss on at least 2 occasions. I have now decided to ditch Excel and use Access 2003 instead. I have imported my master spreadsheet,...
3
23840
by: premMS143 | last post by:
How to append Data to an existing Excel file? means, I'm using VB as front end & once I exported the data to Excel file & saved it as 'ABC.xls'. Now, the question/doubt is; I addded some more records in VB. Now if I want to export these newly updated records to an already existing file 'ABC.xls' what shall I do..? Could anybody help me...? Pls....
0
9721
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
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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
10387
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
10120
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
9200
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
7662
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
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
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.