Import select data from .xls to Access (1 Viewer)

oxicottin

Learning by pecking away....
Local time
Today, 08:23
Joined
Jun 26, 2007
Messages
856
Hello, I have a .xls file that I download every day and would like to import (By button) it into Access BUT the .xls has a lot of gibberish so I only want to gather certain phrases or skip certain words ect. Can someone point me in the correct direction.

Thanks,
 

Isaac

Lifelong Learner
Local time
Today, 05:23
Joined
Mar 14, 2017
Messages
8,777
Hello, I have a .xls file that I download every day and would like to import (By button) it into Access BUT the .xls has a lot of gibberish so I only want to gather certain phrases or skip certain words ect. Can someone point me in the correct direction.

Thanks,
One common approach (and what I would probably recommend - it works for the vast majority of all ETL processes, Access and otherwise) is to import all of it into a 'raw' table. Then, once imported, use Queries to insert "only" the stuff you want into the "permanent" table.

Break it up into a couple steps. Usually, but perhaps not always, it is a good idea to let the power of the database (tables, queries, logic) handle this rather than trying to go "outside" the database and manipulate things on the Excel side. Not that it can't be done.
 

isladogs

MVP / VIP
Local time
Today, 13:23
Joined
Jan 14, 2017
Messages
18,213
I agree.
1. Link to the Excel file
2. Import the raw Excel data into a 'temp' table AKA staging or buffer table
3. Process the data and import what you need into the final table
4. Empty the 'temp' table
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 05:23
Joined
Oct 29, 2018
Messages
21,467
Hi. I'm pretty sure you'll get more specific advice if you could post a sample Excel file and some descriptions of what pieces of data you want to grab from it.
 

oxicottin

Learning by pecking away....
Local time
Today, 08:23
Joined
Jun 26, 2007
Messages
856
@theDBguy, I would but its sensitive data. Thanks for the advise everyone! I'm going to give it a try and if I get into a snag ill just ask for help....

I do however have a question. If I created a table that had phrases like **--STr,,, and text like "TEXT" as an examples and that's the strings I want to remove OR not even import from the excel file how can I do that?

Thanks!
 
Last edited:

Jbooker

New member
Local time
Today, 08:23
Joined
May 12, 2021
Messages
25
Agreed. Always import from source to a temp staging table then clean up and massage into target table(s) as needed.

I would add a boolean (yes/no) field called, say, 'DeleteFlag' to the staging table and flag every row that matches your 'bad phrases' then delete or ignore based on that flag.

An example, flag query would be like:

Code:
UPDATE StagingTable SET DeleteFlag = True WHERE Not 0 = InStr([SomeField],"SomeBadText")

HTH,
Josh
 

oxicottin

Learning by pecking away....
Local time
Today, 08:23
Joined
Jun 26, 2007
Messages
856
Below is what is currently used to retrieve the data from a excel file to a excel file. How can I convert what I have to work in Access?

Code:
Private Sub cmdGetData_Click()

Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
 
'\\Make weak assumption that active workbook is the target
   Set targetWorkbook = Application.ActiveWorkbook

'\\Get the customer workbook
   filter = "xls files (*.xls),*.xls"
   caption = "Please Select Grid Supply Demand Analysis File "
   customerFilename = Application.GetOpenFilename(filter, , caption)

'\\Clear the sheet
   Range("A2:J200").Select
   Selection.ClearContents

'\\Set the font
   ActiveSheet.Range("A2:G200").Font.Name = "Calibri"
   ActiveSheet.Range("A2:G200").Font.Size = 11
   ActiveSheet.Range("A2:G200").Font.ColorIndex = 1 'Black
  
'\\Fit row/ZColumns to header
   Columns("A:J").EntireColumn.AutoFit

'\\Set focus to Cell A1 on Sheet1
    Range("A2").Select

Set customerWorkbook = Application.Workbooks.Open(customerFilename)

'\\Copy data from customer to target workbook
   Dim targetSheet As Worksheet
   Set targetSheet = targetWorkbook.ActiveSheet 'targetWorkbook.Worksheets(1)
   Dim sourceSheet As Worksheet
   Set sourceSheet = customerWorkbook.Worksheets(1)

   targetSheet.Range("A2", "A200").Value = sourceSheet.Range("A4", "A205").Value
   targetSheet.Range("B2", "B200").Value = sourceSheet.Range("C4", "C205").Value
   targetSheet.Range("C2", "C200").Value = sourceSheet.Range("D4", "D205").Value
   targetSheet.Range("D2", "G200").Value = sourceSheet.Range("G4", "J205").Value

'\\Close customer workbook
  customerWorkbook.Close

'\\Auto fit the new columns A,J
   Columns("A:J").EntireColumn.AutoFit
  
'\\Delete Rows starting with "BUYER"
   Call DelBUYERrows
  
'\\Delete Rows starting with "BUYER-COIL"
   Call DelBUYERcoilrows
  
'\\Deletes the string " - SRK -  -  - " (Run first or it will take the SRK and leave dashes)
   Call DelSRKDash
  
'\\Deletes the string "SRK "
   Call DelSRK
  
 '\\Calls AddSpaces to seperate machines
    Call AddSpaces
    
'\\Set focus to Cell A1 on Sheet1
    Range("A2").Select
 
'Save me
    ActiveWorkbook.Save
 
End Sub

Sub DelBUYERrows()
p = Range("A" & Rows.Count).End(xlUp).Row
    For r = p To 1 Step -1
        If Range("A" & r).Value = "BUYER" Then
        Range("A" & r).EntireRow.Delete
        End If
    Next r
End Sub

Sub DelBUYERcoilrows()
n = Range("A" & Rows.Count).End(xlUp).Row
    For r = n To 1 Step -1
        If Range("A" & r).Value = "BUYER-COIL" Then
        Range("A" & r).EntireRow.Delete
        End If
    Next r
End Sub

Sub DelSRKDash()
Columns("C:C").Replace What:=" - SRK -  -  - ", Replacement:="", LookAt:=xlPart
End Sub

Sub DelSRK()
Columns("C:C").Replace What:="SRK ", Replacement:="", LookAt:=xlPart
End Sub
 

Isaac

Lifelong Learner
Local time
Today, 05:23
Joined
Mar 14, 2017
Messages
8,777
The things that were being 'deleted' in Excel will become Queries - and their contained Logic - inside Access.

You'll have to put a little bit of thought and effort into it, but it shouldn't be too hard.
 

Jbooker

New member
Local time
Today, 08:23
Joined
May 12, 2021
Messages
25
The basic building blocks are:
  1. import from Excel into a temp table using docmd.transferspreadsheet (you can specify a saved import spec to refine the data here too)
  2. Execute update queries to delete, replace unwanted strings and 'AddSpaces' using docmd.openquery or docmd.runsql
  3. Now do whatever you need with the data ie: export to excel, massage into permanent tables, print a report, etc.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:23
Joined
Sep 21, 2011
Messages
14,262
As your data is all over the place :( , not consistent in rows and columns as such, you might want to even use an intermediate Excel workbook to put the data into a format that it can be either imported or just linked to.?
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:23
Joined
Sep 21, 2011
Messages
14,262
Using my phone now, but isn't there a link in my sig to anonymize data?
Can't see sig on Android😔
 

oxicottin

Learning by pecking away....
Local time
Today, 08:23
Joined
Jun 26, 2007
Messages
856
Ok I got the import into a temp table now I have to delete items and move into another table. I attached a sample data DB with what I have so far... Don't mind the module its just for reference from the excel VBA....

I have to Delete data in this order now:


1) Delete the Item-Sfx Field
2) Delete the S Field
3) Delete the State Field
4) Delete Rows starting with "BUYER"
5) Delete Rows starting with "BUYER-COIL"
6) Delete the string " - SRK - - - " (Run first or it will take the SRK and leave dashes)
7) Delete the string "SRK "
 

Attachments

  • Database1.zip
    33.8 KB · Views: 196

Isaac

Lifelong Learner
Local time
Today, 05:23
Joined
Mar 14, 2017
Messages
8,777
Sounds like you have a good start on well defined logic, good job.

Let us know how the query creating goes as you begin to create those delete queries (or just select certain records for insert to final table)
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:23
Joined
Sep 21, 2011
Messages
14,262
Ok I got the import into a temp table now I have to delete items and move into another table. I attached a sample data DB with what I have so far... Don't mind the module its just for reference from the excel VBA....

I have to Delete data in this order now:


1) Delete the Item-Sfx Field
2) Delete the S Field
3) Delete the State Field
4) Delete Rows starting with "BUYER"
5) Delete Rows starting with "BUYER-COIL"
6) Delete the string " - SRK - - - " (Run first or it will take the SRK and leave dashes)
7) Delete the string "SRK "
Surely if you run 4, it will take out 5? :unsure:
 

oxicottin

Learning by pecking away....
Local time
Today, 08:23
Joined
Jun 26, 2007
Messages
856
Ok I am running into an error on my cleanup on the import.
Run-time error '3061'

Too few parameters. Expected 1.

Its the mod_RemoveOffendingRows for some reason giving me the error. My other module that I dont have running I commented it out untill I get this one working, it works fine.

Debug below:

error.jpg
 

Attachments

  • Database1 v2.zip
    37 KB · Views: 197

Isaac

Lifelong Learner
Local time
Today, 05:23
Joined
Mar 14, 2017
Messages
8,777
maybe needs single quote delimiter?
 

isladogs

MVP / VIP
Local time
Today, 13:23
Joined
Jan 14, 2017
Messages
18,213
Agree with @Isaac
As it refers to PhraseDelete it is presumably text so...

Code:
strSql = "DELETE * FROM tbl_ImportedGridSupplyDemandAnalysis WHERE [Planner Code]= '" & rst!PhraseDelete & "';"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:23
Joined
Feb 28, 2001
Messages
27,167
@Isaac - probably correct.

Code:
strSQL = "DELETE * FROM tbl_ImportedGridSupplyDemandAnalysis WHERE [Planner Code]='" & rst!PhraseDelete & "';"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:23
Joined
Feb 28, 2001
Messages
27,167
Looks like Colin and I had the same idea at the same time.
 

Users who are viewing this thread

Top Bottom