9

我已经用谷歌搜索了这个错误,但还没有得出我为什么会收到这个错误的结论。我正在尝试用一些数据填充 DataGridView。这是我的代码。

Private Sub LoadGrid()
    Dim cmd As New OleDbCommand
    Dim dt As DataTable
    With cmd
        .CommandText = "project_master_query"
        .CommandType = CommandType.StoredProcedure
        .Connection = New OleDbConnection(My.Settings.cnnString)
    End With
    dt = GetData(cmd)
    dgvData.DataSource = dt
End Sub

Private Function GetData(ByVal cmd As OleDbCommand) As DataTable
    Dim dt As New DataTable
    Using cmd.Connection
        cmd.Connection.Open()
        dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection))
    End Using
    Return dt
End Function

查询存储在 Access 中的“project_master_query”。

    SELECT project_master.*, location_master.LocationName,
    project_currentmilestonedef.MilestoneDefID, 
    project_currentmilestonedef.MilestoneName, project_regions.RegionName,
    owner_fullname.FullName AS OwnerFullName, designer_fullname.FullName AS DesignerFullName,
    project_issuecount.HasOpenIssues, project_updated_closedate.UpdatedCloseDate, 
    project_bonusdays.BonusDays, project_bonusdays.IsGreen, project_bonusdays.IsYellow, 
    project_bonusdays.IsRed, checklist_days_perproject_defined_1.Week1, 
    checklist_days_perproject_defined_1.Week2, checklist_days_perproject_defined_1.Week3, 
    checklist_days_perproject_defined_1.Week4, project_issueduration.ProjectIssueDurationDays, 
    project_active_status.ProjectIsOpen, project_requirement_status.RequirementStatusName, 
    priority_def.PriorityShortName
    FROM project_requirement_status 
    RIGHT JOIN (project_regions 
    RIGHT JOIN (priority_def RIGHT JOIN (location_master 
    RIGHT JOIN ((((checklist_days_perproject_defined AS checklist_days_perproject_defined_1 
    RIGHT JOIN ((((((((contacts_fullname AS designer_fullname 
    RIGHT JOIN (contacts_fullname AS owner_fullname 
    RIGHT JOIN project_master ON owner_fullname.ContactID = project_master.ContactOwner) 
    ON designer_fullname.ContactID = project_master.ContactDesigner) 
    LEFT JOIN project_issuecount 
    ON project_master.ProjectID = project_issuecount.ProjectID) 
    LEFT JOIN project_currentmilestonedef 
    ON project_master.ProjectID = project_currentmilestonedef.ProjectID) 
    LEFT JOIN project_within_benchmark_week1 
    ON project_master.ProjectID = project_within_benchmark_week1.ProjectID) 
    LEFT JOIN project_within_benchmark_week2 
    ON project_master.ProjectID = project_within_benchmark_week2.ProjectID) 
    LEFT JOIN project_within_benchmark_week3 
    ON project_master.ProjectID = project_within_benchmark_week3.ProjectID) 
    LEFT JOIN project_updated_closedate 
    ON project_master.ProjectID = project_updated_closedate.ProjectID) 
    LEFT JOIN checklist_days_perproject_defined 
    ON project_master.ProjectID = checklist_days_perproject_defined.ProjectID) 
    ON checklist_days_perproject_defined_1.ProjectID = project_master.ProjectID) 
    LEFT JOIN project_issueduration 
    ON project_master.ProjectID = project_issueduration.ProjectID) 
    LEFT JOIN project_active_status 
    ON project_master.ProjectID = project_active_status.ProjectID)    
    LEFT JOIN project_bonusdays ON project_master.ProjectID = project_bonusdays.ProjectID) 
    ON location_master.LocationID = project_master.Location) 
    ON priority_def.PriorityDefID = project_master.ProjectPriority) 
    ON project_regions.RegionID = project_master.Region) 
    ON project_requirement_status.RequirementStatusID = project_master.RequirementStatus;

如您所见,查询中根本没有 Nz,所以我不明白为什么会发生这种情况。一旦调用 dt.Load,就会发生错误。

4

2 回答 2

23

Nz函数最有可能出现在您在众多联接之一中引用的视图/查询中。你必须仔细检查所有这些。

作为Nz()Access 应用程序而不是 Access 驱动程序的功能,只要您尝试从 Access 应用程序外部使用它,它就会失败。您可以将 替换为Nz构造IIf(IsNull())

请参阅文档IIfIsNull

放在一起时:

Nz(expr, [valueifnull])

变成

IIf(IsNull(expr), valueifnull, valueifnotnull)

例子

默认:Nz(tbl.A) => IIf(IsNull(tbl.A), '', tbl.A)

有后备:Nz(tbl.A, tbl.B) => IIf(IsNull(tbl.A), tbl.B, tbl.A)

于 2013-01-29T13:53:49.293 回答
0

我创建了一个在这种情况下使用的函数。我叫它ez。

Public Function ez(ByVal vVal1 As Variant, Optional ByVal vVal2 As Variant = 0) As Variant
    If VBA.IsNull(vVal1) Or VBA.Len(vVal1) = 0 Then
        ez = vVal2
    Else
        ez = vVal1
    End If
End Function

正如你所看到的,它比原始函数更进一步,因为它不仅考虑第二个值是否第一个为空,而且如果它为空

此外,默认返回值为 0,而 Nz 为空字符串。

于 2019-12-08T11:36:40.863 回答