Armitage2k
Registered User.
- Local time
- Today, 12:01
- Joined
- Oct 10, 2009
- Messages
- 34
Hi,
i run into a type missmatch error while I am trying to import data from a closed xls workbook (with success), but get the error while trying to replace content of a memo field in the newly imported table.
Sure its just a minor thing, but I just cant get it to work.
Thanks for the support!
A2k
i run into a type missmatch error while I am trying to import data from a closed xls workbook (with success), but get the error while trying to replace content of a memo field in the newly imported table.
Code:
Sub GetDataFromClosedWorkbook()
Dim xlApplication As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim db As Database, rec As recordset
Dim Filename As Object
Dim Selection, Category, Finished As String
' Pick a file to import
Set Filename = Application.FileDialog(msoFileDialogFilePicker)
With Filename
.InitialView = msoFileDialogViewDetails:
.InitialFileName = "C:\":
.Filters.Clear:
.Filters.Add "Pick .xls File", "*.xls", 1:
.ButtonName = "Import file":
.Title = "Search for Database_Import.xls file"
' If no file is selected, close the sub, else keep going
If .Show = -1 Then
Selection = .SelectedItems(1)
Else:
Exit Sub
End If
End With
' Show messagebox to select which entries to import
Category = InputBox("Which worksheet do you want to import?" & Chr(10) & _
"VIP - PCP - LSG - OCT - IHG", "Select Entries", "VIP")
Set xlApplication = CreateObject("Excel.Application")
Set xlWorkbook = xlApplication.Workbooks.Open(Selection, True, True)
Set xlWorksheet = xlWorkbook.Worksheets(Category) ' define which worksheet shall be imported
Set db = CurrentDb
Set rec = db.OpenRecordset(Category) ' define which table shall be opened
' clear selected table
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE " & Category & ".* FROM " & Category & ";"
DoCmd.SetWarnings True
' open the source workbook, read only
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, Category, Selection, True
' close the source workbook without saving any changes
xlWorkbook.Close False
'replace * with: Linebreak + Space + * in column Notes
[B][COLOR=red]StarUpdate_query = "Update Category![Notes] = Replace([Notes], " * ", Chr(10) & Chr(42) & Chr(32))"[/COLOR][/B]
'StarUpdate_query = "UPDATE" & Category
'StarUpdate_query = StarUpdate_query & "SET [Notes] = Replace([Notes], " * ", Chr(13) & Chr(10))"
DoCmd.RunSQL StarUpdate_query
' show alert that process was sucessful
Finished = MsgBox("Import of " & Category & " successfully finished", vbOKOnly, "Finished")
Set xlWorkbook = Nothing ' free memory
rec.Close
End Sub
Sure its just a minor thing, but I just cant get it to work.
Thanks for the support!
A2k
Last edited: