首页 > 解决方案 > 自动数据输入表

问题描述

试图获得帮助以找出为什么我的数据表单没有正确链接到我的数据库。我对此很陌生,并尝试多次遵循教程来获得我需要的东西,但是一路走来,无论我进行什么调整,我都会遇到问题。

这是表单的样子: 在此处输入图像描述

连接到一个 12 列的 Excel 数据库。工作簿中只有 2 张工作表(主页和数据库)不断收到以下错误:

  1. 运行时错误“380”“无法设置 RowSource 属性。无效的属性值。” 在“数据库”区域中应该有一个表单的迷你预览,但它没有显示出来。

这是我的代码:

Option Explicit


Sub Reset()

    Dim iRow As Long
    
    iRow = [Counta(Database!A:A)] 'identifies the last row
    
    With frmForm
    
         .txtID.Value = " "
         .txtName.Value = " "
         .txtCostc.Value = " "
         .txtDept.Value = " "
         .txtPay.Value = " "
         .txtSdate.Value = " "
         .txtSuper.Value = " "
         .optCWS.Value = False
         .optFWS.Value = False
         
         .lstDatabase.ColumnCount = 12
         .lstDatabase.ColumnHeads = True
         
         .lstDatabase.ColumnWidths = "30,60,75,40,60,45,55,60,60,60,60"
         
        If iRow > 1 Then
        
            .lstDatabase.RowSource = "Database!A2:K" & iRow
        Else
        
            .lstDatabase.RowSource = "Database!A2:K"
            
        End If
            
    
    End With
    
End Sub

Sub Submit()

Dim sh As Worksheet
Dim iRow As Long

Set sh = ThisWorkbook.Sheets("Database")

iRow = [Counta(Database!A:A] + 1

With sh

    .Cells(iRow, 1) = iRow - 1
    
    .Cells(iRow, 2) = frmForm.txtID.Value
    
    .Cells(iRow, 3) = frmForm.txtName.Value
    
    .Cells(iRow, 4) = frmForm.txtSuper.Value
    
    .Cells(iRow, 5) = frmForm.txtDept.Value
    
    .Cells(iRow, 6) = IIf(frmForm.optFWS.Value, "S09996", "S09992")
    
    .Cells(iRow, 7) = IIf(frmForm.optCWS.Value, "S09992", "S09996")
    
    .Cells(iRow, 8) = frmForm.txtCostc.Value
    
    .Cells(iRow, 9) = frmForm.txtSdate.Value
    
    .Cells(iRow, 10) = frmForm.txtPay.Value
    
    .Cells(iRow, 11) = Application.UserName
    
    .Cells(iRow, 12) = [Text(Now(), "DD-MM-YYY HH:MM:SS")]
    
End With

End Sub

Sub Show_Form()

    frmForm.Show
    
End Sub

调试器不断将我带到显示表单部分: 在此处输入图像描述

有人知道我在做什么错吗?

标签: excelvbaformsdata-entry

解决方案


VBA 错误本身就非常复杂,说明了Rowsource Property. 在您的代码中,错误应该在这里:

.lstDatabase.RowSource = "Database!A2:K"

显然在 之后缺少一些东西K,例如:

.lstDatabase.RowSource = "Database!A2:K1"

推荐阅读