*EDIT: see 3rd post for solution/solvation
Hi,
Right now I am building a database with items, coming from local dealers.
We send them a locked excel sheet, which they feed with dealer information and item information. In the excel sheet, this is being separated (in the background) in 2 different sheets. I have my master sheet, which is being maintained by the dealer, and I have 2 slave sheets, which I import into Access.
In my first steps into VBA I learned to copy/paste code to import those files into 1 table. (See code below).
I am a virtual VBA n00b, so know nothing about it, yet.
I will have to, though, since I plan to set up this database in VBA.
Now I have 3 challenges:
Who can help me out in this?
Thanks in advance,
Timoo
Hi,
Right now I am building a database with items, coming from local dealers.
We send them a locked excel sheet, which they feed with dealer information and item information. In the excel sheet, this is being separated (in the background) in 2 different sheets. I have my master sheet, which is being maintained by the dealer, and I have 2 slave sheets, which I import into Access.
In my first steps into VBA I learned to copy/paste code to import those files into 1 table. (See code below).
I am a virtual VBA n00b, so know nothing about it, yet.
I will have to, though, since I plan to set up this database in VBA.
Code:
[FONT=Arial]Sub Link_To_Excel()[/FONT]
[FONT=Arial]'Macro Loops through the specified directory (strPath)[/FONT]
[FONT=Arial]'and links ALL Excel files as linked tables in the Access Database.[/FONT]
[FONT=Arial]Const strPath As String = "\\serverpath\New Files for upload\" 'Directory Path[/FONT]
[FONT=Arial]Dim strFile As String 'Filename[/FONT]
[FONT=Arial]Dim strFileList() As String 'File Array[/FONT]
[FONT=Arial]Dim intFile As Integer 'File Number[/FONT]
[INDENT][FONT=Arial]'Loop through the folder & build file list[/FONT]
[FONT=Arial]strFile = Dir(strPath & "*.xlsx")[/FONT]
[FONT=Arial]While strFile <> ""[/FONT]
[FONT=Arial][/FONT]
[FONT=Arial]'add files to the list[/FONT]
[FONT=Arial]intFile = intFile + 1[/FONT]
[FONT=Arial]ReDim Preserve strFileList(1 To intFile)[/FONT]
[FONT=Arial]strFileList(intFile) = strFile[/FONT]
[FONT=Arial]strFile = Dir()[/FONT]
[FONT=Arial]Wend[/FONT]
[FONT=Arial][/FONT]
[FONT=Arial]'[/FONT][FONT=Arial]see if any files were found[/FONT]
[FONT=Arial]If intFile = 0 Then[/FONT]
[FONT=Arial]MsgBox "No files found"[/FONT]
[FONT=Arial]Exit Sub[/FONT]
[FONT=Arial]End If[/FONT]
[/INDENT][INDENT][FONT=Arial]'cycle through the list of files & import to Access[/FONT]
[FONT=Arial]'appending to tables called DealerLists and DealerContacts[/FONT]
[FONT=Arial]For intFile = 1 To UBound(strFileList)[/FONT]
[/INDENT][INDENT][INDENT][FONT=Arial]DoCmd.TransferSpreadsheet acImport, , _[/FONT]
[FONT=Arial]"DealerLists", strPath & strFileList(intFile), True, [B]"parts!A1:E229"[/B][/FONT]
[FONT=Arial]DoCmd.TransferSpreadsheet acImport, , _[/FONT]
[FONT=Arial]"DealerContacts", strPath & strFileList(intFile), True, [B]"contact!A1:F2"[/B][/FONT]
[/INDENT][FONT=Arial]Next[/FONT]
[/INDENT][FONT=Arial]MsgBox UBound(strFileList) & " Files were Imported"[/FONT]
[FONT=Arial][/FONT]
[FONT=Arial]End Sub[/FONT]
Now I have 3 challenges:
- One is, to know up front which dealer (information) is being imported. In the DealerContacts table, I have a lot of dealers. And if I get a new sheet of an existing dealer, I want to know if it already exists in the list or not. I want to be able to refresh the information, if the dealer already exists.
- How do I read out a specific cell, to be able to decide whether I should update or append the information?
- And how do I update the record, if it already exists?
- The second is, to remove old information in the item table and append new information.
- Once I have the dealer number, recorded in the previous session, how can I use this to delete all related records in an item list?
- Kind of: build a query, related to this dealer number, and delete all records with this specific number?
- Last but not least: to make sure I do not import empty cells (dealers have 228 records to their availability, but will seldom use that), I have a counter in sheet 4. This sheet contains only title and a number; number of lines filled with data.
- Essentially this is the same problem as 1., because I need to feed the information of this specific cell to a variable, which I then use to set the TransferSpreadsheet Command with the right amount of lines: import "parts!A1:E" & NumberOfLines
- This is the code I was trying, but it doesn't seem to work (fails at first line):
Code:
[FONT=Arial]'Read the number of lines in the file [/FONT]
[FONT=Arial]Dim xl As Excel.Application[/FONT]
[FONT=Arial]Dim xlsht As Excel.worksheet[/FONT]
[FONT=Arial]Dim xlWrkBk As Excel.Workbook[/FONT]
[FONT=Arial]Dim xlCell As Double[/FONT]
[FONT=Arial]Set xl = CreateObject("Excel.Application")[/FONT]
[FONT=Arial]Set xlWrkBk = GetObject(strPath & strFileList(intFile))[/FONT]
[FONT=Arial]Set xlsht = xlWrkBk.Worksheets("Counters")[/FONT]
[FONT=Arial]Set xlCell = xlsht.cells(2, "A")[/FONT]
[FONT=Arial]DoCmd.TransferSpreadsheet acImport, , _[/FONT]
[FONT=Arial]"DealerLists", strPath & strFileList(intFile), True, [B]"parts!A1:E"[/B] [B]& NumberOfLines[/B][/FONT]
[FONT=Arial]Set xl = Nothing[/FONT]
[FONT=Arial]Set xlWrkBk = Nothing[/FONT]
[FONT=Arial]Set xlsht = Nothing[/FONT]
[FONT=Arial]Set xlCell = Nothing [/FONT]
Who can help me out in this?
Thanks in advance,
Timoo
Last edited: