上传到Access数据库

时间:2016-06-06 14:01:38

标签: excel vba ms-access access-vba ms-access-2003

我尝试将用户输入与现有的Excel文件数据结合起来,这样当它们上传到Access数据库时,它们都将包含在单个表中。

这是我的布局方式,但是我可以随时改变它。

enter image description here

我完全坚持下一步该做什么。上传日期会自动填写,但其余参数将根据产品而有所不同,必须手动填写。我还希望每个字段都必须填写,因此请输入错误消息"输入所有参数"或类似的东西,如果他们不是,这将不允许上传完成。

为什么在Access中只需要这样做而不是Excel,这是因为Excel文件是由AutoCad Electrical生成的,并且它可以包含哪些数据。我尝试在Excel中添加列并导入它们并且工作正常,但我的老板说我们需要用户输入框以使事情变得更容易。

这是我必须导入Excel文件并将其添加到正确的表(_MCL UPLOAD)的代码。现在我只希望能够将用户输入添加到此表作为额外列:

Private Sub ImportMCL_Click()

On Error GoTo ErrorHandler
'disable ms access warnings
DoCmd.SetWarnings False

'load spreadsheet
DoCmd.TransferSpreadsheet acImport, 8, "_MCL UPLOAD", selectFile(), True
MsgBox "MCL Imported Successfully!"
're-enable ms access warnings
DoCmd.SetWarnings True

Exit Sub

ErrorHandler:
MsgBox "There was an Error: " & Err & ": " & Error(Err)


End Sub

Function selectFile()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    If .Show Then
        selectFile = .SelectedItems(1)
    Else
        'stop execution if nothing selected
        End
    End If
End With

Set fd = Nothing
End Function

1 个答案:

答案 0 :(得分:0)

我的最终代码如下:

'Import MCL Files Code
Private Sub ImportMCL_Click()

On Error GoTo ErrorHandler
'disable ms access warnings
DoCmd.SetWarnings False

'load spreadsheet in .xls format
DoCmd.TransferSpreadsheet acImport, 8, "_MCL_UPLOAD", selectFile(), True
DoCmd.OpenQuery "UpdateMCL"
Call InsertInto_MASTER_UPLOAD
Call Delete_MCL_UPLOAD
MsgBox "MCL Imported Successfully!"
're-enable ms access warnings
DoCmd.SetWarnings True

Exit Sub

ErrorHandler:
MsgBox "There was an Error: " & Err & ": " & Error(Err)

End Sub

'Function called in Import MCL Code above
Function selectFile()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    If .Show Then
        selectFile = .SelectedItems(1)
    Else
        'stop execution if nothing selected
        End
    End If
End With

Set fd = Nothing
End Function

'Function Used to Delete MCL Uploaded file after it's moved to Master Table
Sub Delete_MCL_UPLOAD()

Dim dbs As Database, rst As Recordset

' Modify this line to include the path to Northwind
' on your computer.
Set dbs = OpenDatabase("default_cat.mdb")

' Delete employee records where title is Trainee.
dbs.Execute "DELETE * FROM " _
    & "_MCL_UPLOAD "

dbs.Close

End Sub

'Function Appends _MCL UPLOAD into the _MASTER_UPLOAD table
Sub InsertInto_MASTER_UPLOAD()

Dim dbs As Database

' Modify this line to include the path to Northwind
' on your computer.
Set dbs = OpenDatabase("default_cat.mdb")

' Select all records in the New Customers table
' and add them to the Customers table.
dbs.Execute " INSERT INTO _MASTER_UPLOAD " _
    & "SELECT * " _
    & "FROM [_MCL_UPLOAD];"

dbs.Close

End Sub

我基本上创建了另一个表来将所有组合信息转储到。感谢您的所有帮助!