您應該實現一個導入過程。首先創建一個帶有超鏈接字段的表格,然後將數據從Excel導入到該表格中。
Option Compare Database
Private Sub Command0_Click()
Dim rec As Recordset
Dim db As Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim xlApp As Object 'Excel.Application
Dim xlWrk As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlWrk = xlApp.Workbooks.Open("C:\Users\....\Desktop\EMS Ver3.xlsm") 'Your directory
Set xlSheet = xlWrk.Sheets("SUMMARY") 'your sheet name
Set db = CurrentDb
Set tdf = db.CreateTableDef()
tdf.Name = "My table imported"
'Delete the table if it exists
If TableExists("My table imported") Then
DoCmd.DeleteObject acTable, "My table imported"
End If
'Create table
Set fld = tdf.CreateField("hyperlinking", dbMemo, 150)
fld.Attributes = dbHyperlinkField + dbVariableField
tdf.Fields.Append fld
' append more field here if you want ...
With db.TableDefs
.Append tdf
.Refresh
End With
Set rec = db.OpenRecordset("My table imported")
m = 11 ' Let say your data is staring from cell E11 we will loop over column E until no data is read
Do While xlSheet.Cells(m, 5) <> ""
rec.AddNew
rec("hyperlinking") = xlSheet.Cells(m, 5)
rec.Update
m = m + 1
Loop
End Sub
Public Function TableExists(TableName As String) As Boolean
Dim strTableNameCheck
On Error GoTo ErrorCode
'try to assign tablename value
strTableNameCheck = CurrentDb.TableDefs(TableName)
'If no error and we get to this line, true
TableExists = True
ExitCode:
On Error Resume Next
Exit Function
ErrorCode:
Select Case Err.Number
Case 3265 'Item not found in this collection
TableExists = False
Resume ExitCode
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "hlfUtils.TableExists"
'Debug.Print "Error " & Err.number & ": " & Err.Description & "hlfUtils.TableExists"
Resume ExitCode
End Select
End Function
神奇的是,當你創建一個備註字段並設置其屬性爲超鏈接:
Set fld = tdf.CreateField("hyperlinking", dbMemo, 150)
fld.Attributes = dbHyperlinkField + dbVariableField
tdf.Fields.Append fld
您可以從Excel中複製任何東西到該字段,同時保留的超級鏈接:
rec("hyperlinking") = xlSheet.Cells(m, 5)
這只是一個例子。您需要修改表名,文件目錄,電子表格名稱,字段名稱,如果需要添加更多字段。
太棒了!謝謝你的銀盤上的一切。 我唯一的問題是導入任何字段爲dbNumeric或dbDate或dbCurrency,它總是吐出一個錯誤= /現在我符合導入全部爲dbText。知道我是否做錯了什麼? – user2296381
對於dbNumberic和dbCurrency,該值必須始終包含數值(從0-9),即使「1,234」由於逗號而仍被視爲無效。所以你必須在複製之前先驗證你的數據。嘗試 如果IsNumeric(xlSheet.Cells(m,5).Value)然後 rec(「currency」)= xlSheet.Cells(m,5) – Nexus