473,387 Members | 2,436 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,387 software developers and data experts.

Retrieving data in certain cells in Excel to MS Access usign VBA

48
hello,

been searching for a solution on this but all i get are advices for importing data from excel directly to MS Access DB.

my case here is different...the sheet in Excel file is not ready for importing...
the data are scattered from different cells from different sheets...
this is because these sheets are automated for their own role...

i just need to get data from a certain cell using VBA den save the data to MS Access DB.

Example:
the data are in Sheet 1 in A1, A2, C5, F5...
others are in Sheet 2 in B4, B5, B6, G7...

you see, i need to get each data from them...
then save them to MS Access DB...
the sheet is not ready for db import...

does anyone knows how to retrieve data in each cell?
then save data one-by-one to MS Access DB...
or are there other better ways?
(must not change the format of the excel)


hoping to hear good advices from the experts and the experienced,
keirnus
Aug 11 '08 #1
3 2528
NeoPa
32,556 Expert Mod 16PB
Using Application Automation it's relatively straightforward to get the data from Excel as long as you know where the data is.

Saving "it" into Access though, is just plain meaningless. Where and how do you want these disparate items of data to be saved?
Aug 11 '08 #2
keirnus
48
Using Application Automation it's relatively straightforward to get the data from Excel as long as you know where the data is.

Saving "it" into Access though, is just plain meaningless. Where and how do you want these disparate items of data to be saved?
Thanks NeoPa for the reply.
But the data in Excel are scattered in different cells.

Example:
I have one table in Access called "tblTable".
The columns are Col1, Col2, Col3, Col4, Col5, Col6.
The data to be inserted in this table are in different cells in Excel.
The locations are Sheet1 in A1, A2, C5 then Sheet2 in B4, B5, G7.

Let's say:
[tblTable].Col1 = Sheet1!A1
[tblTable].Col2 = Sheet1!A2
[tblTable].Col3 = Sheet1!C5
[tblTable].Col4 = Sheet2!B4
[tblTable].Col5 = Sheet2!B5
[tblTable].Col6 = Sheet2!G7

How do i get the data in Excel one by one?

I'm sorry but i'm kinda new on this stuff.
But I kinda liking doing this already. =)
Just got bumped on this though. =(

Hope you could help give advices.
Aug 11 '08 #3
NeoPa
32,556 Expert Mod 16PB
Assuming then, that you want to add a new record into the table every time this process is run, you need to include the following logic.

NB. This is not the whole program or code, but it does provide the building blocks you will need.

Firstly, using the Application Automation linked in the earlier post, retrieve the specific cells that you need into six variables that will not lose scope. These variables (or the data in them) will be required later on to insert into the table.

I will assume, for the moment, that the first three items are numeric, string and date data respectively. I doubt your actual data is, but it illustrates how each should correctly be handled. ==> Col1=Numeric; Col2=String; Col3=Date. The remaining three columns are all Numeric. Numeric data should all be processed in a similar way, but which data type you use to store the data in may depend on the precise nature of the data you expect. I will use Double variables for all numeric items in the example.

At the top of a module (where the code to add the data into the table is to be found) you will need to define a constant which is the SQL string used to add in the data. I also include in here the Dim statements for the six variables as I've described.
Expand|Select|Wrap|Line Numbers
  1. Private Const conUpdate As String = _
  2.                   "INSERT INTO tblTable" & _
  3.                   "(Col1,Col2,Col3,Col4,Col5,Col6) " & _
  4.                   "VALUES(%A,%B,%C,%D,%E,%F)"
  5. Private dblC1 As Double
  6. Private strC2 As String
  7. Private datC3 As Date
  8. Private dblC4 As Double
  9. Private dblC5 As Double
  10. Private dblC6 As Double
You will then have some code to get all the data from Excel. We can dig into this a little deeper if you find you need to, but for now I will leave you with the link and expect you to work out the basics of how this is done. I believe all the info you need is there.

After that there will be code to populate a new record with this data. Note very well, the difference in how the different fields are handled. SQL is particularly complicated where dates are concerned. This code is designed for SQL. It does NOT depend in the least on which country the code is run in. SQL date format is global and independent of local formats (See Literal DateTimes and Their Delimiters (#)).
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String, strWork As String
  2.  
  3. 'Insert your code to populate the vales in dblC1 through dblC6
  4.  
  5. strSQL = conUpdate
  6. 'Formate for numeric values
  7. strSQL = Replace(strSQL, "%A", dblC1)
  8. 'Formate for string values
  9. strSQL = Replace(strSQL, "%B", "'" & strC2 & "'")
  10. 'Formate for numeric values
  11. strSQL = Replace(strSQL, "%C", Format(datC3, '\#m/d/yyyy\#'))
  12. strSQL = Replace(strSQL, "%D", dblC4)
  13. strSQL = Replace(strSQL, "%E", dblC5)
  14. strSQL = Replace(strSQL, "%F", dblC6)
  15.  
  16. Call DoCmd.RunSQL(SQLStatement:=strSQL, UseTransaction:=False)
Aug 12 '08 #4

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

Similar topics

4
by: Martin | last post by:
There is an Access table on the network. 15 users who do not have Access are connected to the network. Is there a way for each user to be able to enter one or more rows containing 3 or 4 columns to...
11
by: Heather | last post by:
I need suggestions on possible ways to implement the following: 10 users on a network need to infrequently add data to a backend database on the network. The data would be a couple of records...
5
by: Jonny | last post by:
Hello, I have created a button on my form which when pressed does the following : 1) Run a pre-defined Macro, which is picking up a query and running my data to excel. However, I need the...
2
by: Chris Bellini | last post by:
Greetings! I'm developing a C# application that needs to read some data from a selected XLS file. I've used VB in the past to automate Excel but this is the first time I've used C#. Back in VB,...
2
by: Hans Wien | last post by:
I have to write a lot of data to excel for analysts who use it further from there but would like to use managed C++ and not C# since a lot of code is involved which already exists now in managed...
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...
0
by: coony | last post by:
Hi everyone, I got an annoying thing going on. I've got an MSSQL db which is filled with different data, used by another program (T-Plan). I should read some table to import in another DB. The...
7
by: semijoyful | last post by:
OS: Win XP SP2 Access version: 2003 Excel version: 2003 I am new at this, as I am sure you have gathered from this post title:) I am working on a form where users can input data in Access and...
1
by: rishiyo | last post by:
Good ones!! I want to retrieve and add datas with excel. I've tried one by adding two textboxes and two command buttons. THE code is: 'do declare these variables you need to add a reference'to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...

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.