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

Importing an excel file in Access via VBA

19
I have an access databae that I am creating to import data from the end user from an excel file. And from that generate a report for him to send to a customer. The issue that I am having the report has a group by section based off one of the fields and the field is based off a custom list of options. I.E. not numerical or alphebetical. So what I am trying to do is convert the values of the custom list to a numerical list to provide for easy grouping in the report. And I am having trouble doing this. I can import the data directly but I cant change it on the fly. I was thinking of using a SQL script to input into the table but I dont know how to loop through the excel file to import it into the table. is there a better way to do this or could some one give me some advice on how to do this. the output sql would look somethign like this

select data1, IIF(data2="somevalue",0,data2="somevalue",1,2) as data2 from <excel file>

any help would be greatly appreciated.

CG
Mar 29 '07 #1
8 2241
MMcCarthy
14,534 Expert Mod 8TB
Hi CG,

Try this ...
Expand|Select|Wrap|Line Numbers
  1. select data1, IIf(data2="somevalue",0,IIf(data2="somevalue",1,2)) as data2 from <excel file>
  2.  
Mar 29 '07 #2
cgrider
19
Hi CG,

Try this ...
Expand|Select|Wrap|Line Numbers
  1. select data1, IIf(data2="somevalue",0,IIf(data2="somevalue",1,2)) as data2 from <excel file>
  2.  

Thank you for the response I miss- typed my statement what I am having problems with is stepping through the excel file record by record. the way I am doing the excel import now is using the docmd.importexcel command built into access which does not allow for me to convert values on the fly.


BTW i am using Access 2003
Mar 30 '07 #3
Denburt
1,356 Expert 1GB
If you view this thread it may help solve your problem using a schema you can change the values to one type or another then make any further adjustments once in the database table.

http://www.thescripts.com/forum/thre...2800-2-10.html

Now if you truly want to open excel and scroll line by line I can post that as well. I hope you know a little about coding though since this is a bit more complicated. You will need to set a reference (Tools, Reference) in your VBA window to Microsoft Excel. This will give you the most control however there is also one below that does not require the Excel reference and they are similar in nature.

Expand|Select|Wrap|Line Numbers
  1.   Dim objExcelApp As Excel.Application 
  2.     Dim objWorkbook As Excel.Workbook 
  3.     Dim objWorksheet As Excel.Worksheet 
  4.  
  5.     strPath = ThisWorkbook.Path 'sets the path of the file for use later
  6.  
  7.     Set objExcelApp = New Excel.Application 'sets object as new instance of excel
  8.  

No reference needed in your references:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2. Dim i As String
  3. Dim ACell As String
  4. Dim Count As Integer, Count1 As Integer, Count2 As Integer
  5. Dim objXL As Object
  6. Dim objXLWB As Object
  7. Dim objXLWS As Object
  8. Dim MyBook As String
  9. Dim strRange As String
  10. Dim ObjSht As Object
  11.     'MyBook = "C:\MyFolder\MyExcelSheet.xls" ' change to name/path of your workbook
  12.     MyBook = "C:\MyFolder\MyExcelApp.xls"
  13.     Set objXL = CreateObject("Excel.Application")
  14.     Set objXLWB = objXL.workbooks.Open(MyBook)
  15.     Set ObjSht = objXLWB.ActiveSheet
  16.  
  17. Dim EndCell As Integer
  18.  
  19. Count = 1
  20. With ObjSht
  21. ACell = .Cells(Count, 2)
  22.     Do Until ACell = "" And Count2 > 5
  23.      'Debug.Print Cells(Count, 2)
  24.      If .Cells(Count, 2) = "" Then
  25.      .Cells(Count, 2).Select
  26.          Count2 = 0
  27.      Count1 = 2
  28.     ' Debug.Print Cells(Count, 1).Select
  29.      Do Until .Cells(Count, Count1) > "" Or Count2 > 5
  30.          'Debug.Print Cells(Count, Count1)
  31.          If .Cells(Count, Count1) > "" Then Exit Do
  32.          If Count1 > 11 Then
  33.              If .Cells(Count, 2) = "" Then
  34.                  .Range("B" & Count & ":L" & Count).Delete
  35.              Else
  36.                  Count = Count + 1
  37.              End If
  38.               Count1 = 2
  39.              Count2 = Count2 + 1
  40.              'Count = Count - 1
  41.              'Exit Do
  42.          End If
  43.  
  44.  
  45.      Count1 = Count1 + 1
  46.     Loop
  47. End If
  48. 'If count2 > 5 Then Exit Do
  49. ACell = .Cells(Count, 2)
  50. Count = Count + 1
  51. Loop
  52. End With
  53. End Sub
  54.  
This is a smipet of some code I used at one time I think it verifies empty rows and deleted them as it scrolled down once it hit a certain number of blank cells it quit. You will need to refine it as you see fit. Any questions comments let me know.
Mar 30 '07 #4
cgrider
19
thank you very much for the code I will look at it tonight and try to make it work I do appreciate it. I will try both types and let you know on the results

I know a little bit of coding but by no means an expert which was why it was frustrating me so much I know what I wanted it to do just could not figure out how to do it :)
Mar 30 '07 #5
cgrider
19
could you show me the code with the reference to ms excel also? I am a little confused on the code you posted on the cell fields. does the excel file, once loaded, is treated like a table and I can access the record as a whole and access the headers as oppsed to the individual cells? or do I have restrict the user on moving columns around on the excel file. the reason why I am asking was because I was thinking of using the built in function of loading the execl to a tmp table and then running an update query against it to change the values and then copy the new data to the main table and wanted to see if I any of the steps can be trimmed out? any help would be appreciated. I am probably making things harder than they need to be. :) I seem to do that alot. Also what I want to do is use aliases for the field names so the end user can better undersatnd the column headings for when they put in the data and wont be confused with my naming conventions of the table coulmn names.


anyways thanks for your help on this I really do appreciate it.
Apr 2 '07 #6
Denburt
1,356 Expert 1GB
using the built in function of loading the execl to a tmp table and then running an update query against it to change the values and then copy the new data to the main table and wanted to see if I any of the steps can be trimmed out?
Probably your best bet.

I am probably making things harder than they need to be. I seem to do that alot.
Me too.

what I want to do is use aliases for the field names so the end user can better undersatnd the column headings for when they put in the data and wont be confused with my naming conventions of the table coulmn names.
Umm need more coffee will get back to you on this. :)
Apr 3 '07 #7
cgrider
19
ok this is the code that I wrote if you can take a look at it and see if I am on the rigth track or not I did not add all the colums names from the tables to the code here to make it a little more readable. I hope this might help better explain what i am trying to do.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnEnterWarranty_Click()
  2.  
  3. Dim strMessage As String
  4. Dim vrtSelectedItem As Variant
  5. Dim strMyDir As String
  6. Dim strTable As String
  7. Dim strTmpTable As String
  8. Dim strSQL As String
  9. Set dlgopen = Application.FileDialog(msoFileDialogFilePicker)
  10.  
  11. strTmpTable = "tblTmpData"
  12. strTable = "tblwarranty"
  13.  
  14. strForm = "frmWarrantyInput"
  15. strMessage = "Do you have a excel file to import?"
  16. '============================================================
  17.  
  18.  
  19.  
  20. '=================================================================
  21.  
  22. If MsgBox(strMessage, vbYesNo, "External Data") = vbYes Then
  23. 'This is the import of the excel file section
  24.  
  25. With dlgopen
  26.  
  27.     .Title = "Import Warranty Information Data"
  28.     .AllowMultiSelect = False
  29.     .Filters.Add "Excel Files", "*.xls", 1
  30.  
  31.     If .Show = True Then
  32.         For Each vrtSelectedItem In .SelectedItems
  33.         strMyDir = CStr(vrtSelectedItem)
  34.         ' MsgBox strMyDir
  35.  
  36.         Next vrtSelectedItem
  37.  
  38.         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTmpTable, strMyDir, True
  39.         strSQL = "update " & strTmpTable & " set pinstalled = iif('installed',1,iif('Spare',2,3))"
  40.         DoCmd.RunSQL strSQL
  41.         strSQL = "insert into " & strTable & " from (select [service bulletin number] as location, pinstalled from " & strTmpTable
  42.         DoCmd.RunSQL strSQL
  43.  
  44.         MsgBox "The Import is complete"
  45.     Else
  46.         MsgBox "You clicked Cancel"
  47.     End If
  48. End With
  49.  
  50. Else
  51.     DoCmd.Close
  52.     DoCmd.OpenForm strForm
  53. End If
  54.  
  55.  
  56. End Sub
  57.  
any suggestions are always welcomed
Apr 3 '07 #8
Denburt
1,356 Expert 1GB
I think it is looking good however "Next vrtSelectedItem" should follow your transfer method in case they selected more than one file in the dialog box.

Expand|Select|Wrap|Line Numbers
  1.         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTmpTable, strMyDir, True
  2.         strSQL = "update " & strTmpTable & " set pinstalled = iif('installed',1,iif('Spare',2,3))"
  3.         DoCmd.RunSQL strSQL
  4.         strSQL = "insert into " & strTable & " from (select [service bulletin number] as location, pinstalled from " & strTmpTable
  5.         DoCmd.RunSQL strSQL
  6.  Next vrtSelectedItem
  7.  
Apr 3 '07 #9

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

Similar topics

11
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for...
9
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
2
by: nutthatch | last post by:
I want to be able to import an Excel spreadsheet into Access 2K using the macro command Transferspreadsheet. However, the file I am importing (over which I have no control) contains some records...
9
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. ...
1
by: Geoff Jones | last post by:
Hi I have a question which I hope somebody can answer. I have written a VB application with which I want to import an Excel file, analyze the data within it and do some calculations. There are...
1
by: winzy | last post by:
I have a table in Access which needs updating from an Excel file. Instead of importing the whole Excel like a new table in Acess, how do I do a partial import/update?? For example, Access table...
5
by: hharriel | last post by:
Hi, I am hoping someone can help me with an issue I am having with excel and ms access. I have collected data (which are in individual excel files) from 49 different school districts. All...
3
by: mukeshsrivastav | last post by:
dear sir i want to move form excel to access. i have 5 excel file having same formats and fields.now i want to import all data in one access table. importing one file is easy .but importing and...
28
by: kkadakia | last post by:
I get a daily excel file for a entire month which I want to transfer into Access at the end of the month. So, there are around 20-25 excel files I get by the end of the month, and I would like to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...

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.