Ms access 使用ProgressBar access vba时出现用户定义类型未定义错误

Ms access 使用ProgressBar access vba时出现用户定义类型未定义错误,ms-access,vba,Ms Access,Vba,我正在尝试为访问表单实现一个进度条。调用方法时,它抛出一个错误,表示用户定义的类型未定义,并在Form_ProgressBar中进行错误标记 Private Sub exampleCall1() ' example call for using progress bar with a looping process Dim pbar As Form_ProgressBar Dim i As Long Dim steps As Long steps = 100000

我正在尝试为访问表单实现一个进度条。调用方法时,它抛出一个错误,表示用户定义的类型未定义,并在Form_ProgressBar中进行错误标记

Private Sub exampleCall1() ' example call for using progress bar with a looping process  
    Dim pbar As Form_ProgressBar
    Dim i As Long Dim steps As Long 
    steps = 100000    
    ' create new instance of Progress Bar  
    Set pbar = New Form_ProgressBar  
    With pbar  ' #of steps, Mode, Caption  
        .init steps, PBarMode_Percent, "Hey, I'm working here!" 
        For i = 1 To steps  
            ' do something in a loop  
            ' update progress  
            .CurrentProgress = i  
        Next i 
    End With 
    Set pbar = Nothing
End Sub 
下面是调用进度条方法的方法

Public Sub ImportExcelfile(tblname As String, drpdwn As String)

Dim ExcelApp As New Excel.Application
Dim ExcelBook As New Excel.Workbook
Dim rng As Excel.Range
Dim rngDefine As Excel.Range

Dim objDialog As Object
Set objDialog = Application.FileDialog(3)
Dim strXls As String
On Error Resume Next

'Dialog box to select the excel file
     With objDialog
     .Title = "Select the Excel file to import"
     .AllowMultiSelect = False
     .Filters.Clear
     .Filters.Add "Excel Files macros enabled", "*.xlsm", 1
     .Filters.Add "All Files", "*.*", 2
     .Filters.Add "Excel Files", "*.xlsx", 3

If .Show = -1 Then
StrFileName = .SelectedItems(1)

     ExcelApp.Visible = False

     Set ExcelBook = ExcelApp.Workbooks.Open(StrFileName, False, True)

     Set rngDefine = ExcelBook.Worksheets("sheet1").Range("A1:AJ1")

     If IsError(ExcelApp.Match("text1", rngDefine, 0)) Then

        DoCmd.TransferSpreadsheet transfertype:=acImport, _
        tablename:=drpdwn, _
        FileName:=StrFileName, Hasfieldnames:=True, _
        Range:="Sheet1!I:J", SpreadsheetType:=5


        DoCmd.TransferSpreadsheet transfertype:=acImport, _
        tablename:=tblname, _
        FileName:=StrFileName, Hasfieldnames:=True, _
        Range:="Sheet1!A:FK", SpreadsheetType:=5

     Else

MsgBox "File you trying to import contains one heading 'text1' in the first 
row.Please Delete it before importing"

End If

End With
  ExcelBook.Close SaveChanges:=False
 Set ExcelBook = Nothing
ExcelApp.Quit
Set ExcelApp = Nothing

End sub  
在打开工作簿、设置范围和检查excel工作表中的特定文本1时,需要花费时间处理的代码。因为我想显示进度条

    Set ExcelBook = ExcelApp.Workbooks.Open(StrFileName, False, True)

    Set rngDefine = ExcelBook.Worksheets("sheet1").Range("A1:AJ1")

    If IsError(ExcelApp.Match("text1", rngDefine, 0)) Then

如果已重命名进度条表单,则需要更改对象类型

Dim pbar as Form_YourNameHere
Set pbar = Neew Form_YourNameHere
由于代码中没有循环,您需要手动进行一些计算,并自己增加pbar的CurrentProgress属性。我已经修改了你的代码来做这件事。步骤不多,因此进度将“跳跃”一点

Public Sub ImportExcelfile(tblname As String, drpdwn As String)

Dim ExcelApp As New Excel.Application
Dim ExcelBook As New Excel.Workbook
Dim rng As Excel.Range
Dim rngDefine As Excel.Range

Dim objDialog As Object
Set objDialog = Application.FileDialog(3)
Dim strXls As String

Dim pbar As Form_ProgressBar 'or whatever you named it

On Error Resume Next

'Dialog box to select the excel file
With objDialog
    .Title = "Select the Excel file to import"
    .AllowMultiSelect = False
    .Filters.Clear
    .Filters.Add "Excel Files macros enabled", "*.xlsm", 1
    .Filters.Add "All Files", "*.*", 2
    .Filters.Add "Excel Files", "*.xlsx", 3

If .Show = -1 Then
    StrFileName = .SelectedItems(1)

    ExcelApp.Visible = False

    Set pbar = New Form_ProgressBar 'again, whatever you named the form
    'There are 5 distinct steps to this code.
    pbar.init 5, PBarMode_Percent

    Set ExcelBook = ExcelApp.Workbooks.Open(StrFileName, False, True)

   'increment pbar
    pbar.CurrentProgress = 1 '20%

    Set rngDefine = ExcelBook.Worksheets("sheet1").Range("A1:AJ1")

    pbar.CurrentProgress = 2 '40%
    If IsError(ExcelApp.Match("text1", rngDefine, 0)) Then

        DoCmd.TransferSpreadsheet transfertype:=acImport, _
        tablename:=drpdwn, _
        fileName:=StrFileName, Hasfieldnames:=True, _
        Range:="Sheet1!I:J", SpreadsheetType:=5

        'increment pbar
        pbar.CurrentProgress = 3 '60%

        DoCmd.TransferSpreadsheet transfertype:=acImport, _
        tablename:=tblname, _
        fileName:=StrFileName, Hasfieldnames:=True, _
        Range:="Sheet1!A:FK", SpreadsheetType:=5

        'increment pbar
        pbar.CurrentProgress = 4 '80%
    Else
        ' remove progress bar on "error"
        Set pbar = Nothing

        MsgBox "File you trying to import contains one heading 'text1' in the first row.Please Delete it before importing"

    End If

End With
ExcelBook.Close SaveChanges:=False
Set ExcelBook = Nothing
xcelApp.Quit
Set ExcelApp = Nothing

'all done
pbar.CurrentProgress = 5 '100%
Set pbar = Nothing
End Sub

对于任何一个绊倒在这上面的人。OP正在实现我最初发布在这里的MS Access ProgressBar表单

你好@vuyy1182。我写的代码。我很乐意帮忙。它突出显示了哪一行代码?你把你的进度条命名为“进度条”了吗?@ckuhn203,很巧。{Dim pbar As Form_ProgressBar}和{Set pbar=New orm_ProgressBar}导致错误。我重新命名了它。我有一个疑问,在任何情况下,我是否可以调用进度条方法,例如,当尝试从excel导入内容到access时,我需要调用该方法。确定。如果重命名了它,则需要更改对象类型。{Dim pbar作为形式u“whateveryounamedit”}和{Set pbar=New形式“whateveryounamedit”}。实际上,您正在创建特定表单对象的新实例。是的,您应该能够随时调用pbar表单的新实例。这听起来几乎完全是我设计它的原因的翻版。但在我的情况下,我不会在将内容传输到access时循环,但在上面的方法中,它必须让循环执行步骤并更新进度。在我的情况下如何调用该方法。我将在调用progressbar方法的地方发布我的代码。还有一件事,当它加载消息周围的文本时,消息显示为黑色。如何删除?如果我理解正确,您需要在表单设计模式中将文本框的“back style”属性设置为“transparent”。我将其设置为Transperant。Public Sub init()方法,Me.Text0=“Ready”。。仅在文本周围显示黑色。