crownedzero
Registered User.
- Local time
- Today, 07:29
- Joined
- Jun 16, 2009
- Messages
- 54
So I'm hacking and slashing my way to learn VBA and I've come across something that no clue to figure out. I have a Msgbox that prompts the user to import a file ("Yes") or opens the file to be edited ("No") and then (hopefully) upon saving and closing the Excel spreadsheet I'd then like it to be imported. Possible?
Be warned I'm sure this is some ugly code and tips/pointers are welcome.
Be warned I'm sure this is some ugly code and tips/pointers are welcome.
Code:
Public Function Import_Inventory()
Dim Msg As String, Button As Variant, Title As String, Response As Variant, fDialog As FileDialog
Dim strFileName As String, XL As Object
Msg = "Have you removed any duplicates and blank spaces from the file you are importing?"
Button = vbYesNo + vbDefaultButton2
Title = "Import File"
Response = MsgBox(Msg, Button, Title)
If Response = vbNo Then
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
Set XL = CreateObject("Excel.Application")
With fDialog
.InitialFileName = "N:\shared\Lori\BSLR"
.AllowMultiSelect = False
.Title = "Please select file to edit"
.Filters.Clear
.Filters.Add "Excel", "*.xls"
.Filters.Add "All Files", "*.*"
If .Show = True Then
strFileName = .SelectedItems(1)
XL.Workbooks.Open strFileName
XL.Visible = True
End If
End With
Else
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.InitialFileName = "N:\shared\Lori\BSLR"
.AllowMultiSelect = False
.Title = "Please select file to import"
.Filters.Clear
.Filters.Add "Excel", "*.xls"
.Filters.Add "All Files", "*.*"
If .Show = True Then
strFileName = .SelectedItems(1)
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblInventory"
DoCmd.TransferSpreadsheet , acSpreadsheetTypeExcel9, "tblTemp", strFileName, True
DoCmd.RunSQL "INSERT INTO tblInventory SELECT * FROM tblTemp"
DoCmd.DeleteObject acTable, "tblTemp"
DoCmd.RunSQL "UPDATE tblInventory SET [Available]=0 WHERE [Available]<0"
DoCmd.SetWarnings True
End If
End With
End If
End Function