2017-11-18 142 views
0

我tryng以避免经由下面 enter image description here格式化的“Screen.ActiveDatasheet”的控制与VBA

每图像查询构建器德兴设置属性这行代码(打开查询)之后

DoCmd.OpenQuery cstrQueryName, acViewNormal, acReadOnly 

我想动态格式化的列(如更改标题,当它是一个数字的格式...)

我曾尝试以下但都失败了

Screen.ActiveDatasheet.Controls(0).Caption.Value = "Assembly" 
Screen.ActiveDatasheet.Controls(0).Properties("Caption").Value = "Assembly" 
Screen.ActiveDatasheet.Controls(0).Field.Properties("Caption").Value = "Assembly" 

这里完整的源

Sub ViewASSYPer(TypeofCode As String) ' 
    Const cstrQueryName As String = "ViewASSYPerType" 
    Dim db As DAO.Database 
    Dim qdf As DAO.QueryDef 
    Dim sqlStr As String 

    On Error GoTo ViewASSYPer_Err: 
    CloseDeletequery cstrQueryName 

Set db = CurrentDb 

'rebuild query 

sqlStr = "SELECT ASSY_TABLE.assy_number, ASSY_TABLE.revision, ASSY_TABLE.description," 
sqlStr = sqlStr & " ASSY_TABLE.target_weight, assy_weight_table.assy_weight, assy_weight_table.assy_fs, assy_weight_table.assy_bl, assy_weight_table.assy_wl" 
sqlStr = sqlStr & " FROM ASSY_TABLE INNER JOIN assy_weight_table ON ASSY_TABLE.assy_number = assy_weight_table.assy_number " 
sqlStr = sqlStr & " WHERE (((ASSY_TABLE.CollectorTypeCode) = """ & TypeofCode & """));" 

Set qdf = db.CreateQueryDef(cstrQueryName) 
qdf.SQL = sqlStr 

DoCmd.OpenQuery cstrQueryName, acViewNormal, acReadOnly 

Screen.ActiveDatasheet.AutoResize = True 

' 'Screen.ActiveDatasheet.Controls(1).Properties("Caption").Value = "Rev:" 
' Screen.ActiveDatasheet.Controls(1).Field.Properties("Caption").Value = "Description" 
' Screen.ActiveDatasheet.Controls(3).Field.Properties("Caption").Value = "Target Weight" 
' Screen.ActiveDatasheet.Controls(4).Field.Properties("Caption").Value = "Weight:" 
' Screen.ActiveDatasheet.Controls(5).Field.Properties("Caption").Value = "FS Arm" 
' Screen.ActiveDatasheet.Controls(6).Field.Properties("Caption").Value = "BL Arm" 
' Screen.ActiveDatasheet.Controls(7).Field.Properties("Caption").Value = "WLArm" 
' 
' Screen.ActiveDatasheet.Controls(0).Field.Properties("Format") = "Fixed" 
' Screen.ActiveDatasheet.Controls(0).Field.Properties("DecimalPlaces") = 3 

    Screen.ActiveDatasheet.Controls(0).SetFocus 
    DoCmd.RunCommand acCmdFreezeColumn 

Set qdf = Nothing 
Set db = Nothing 

    Exit Sub 

ViewASSYPer_Err_Exit: 
    Exit Sub 

ViewASSYPer_Err: 
    MsgBox Error$ 
    Resume ViewASSYPer_Err_Exit 

End Sub 

回答

0

尝试用:

Screen.ActiveDatasheet.Controls(0).Caption = "Assembly" 

或者创建和datasheetview使用表格查询源。

0

我的确解决了我的问题。在QueryDef上应用了属性。 但在这些代码行中仍然存在一些“未找到属性”的问题。

qdf.Fields(4).Properties("Caption").Value = "Total Weight:" 
qdf.Fields(5).Properties("Caption").Value = "FS Arm" 
qdf.Fields(6).Properties("Caption").Value = "BL Arm" 
qdf.Fields(7).Properties("Caption").Value = "WL Arm" 

qdf.Fields(5).Properties("Format") = "Fixed" 
qdf.Fields(6).Properties("Format") = "Fixed" 
qdf.Fields(7).Properties("Format") = "Fixed" 

这里更新了源代码。

Sub ViewASSYPer(TypeofCode As String) ' 
    Const cstrQueryName As String = "ViewASSYPerType" 
    Dim db As DAO.Database 
    Dim qdf As DAO.QueryDef 
    Dim sqlStr As String 

    On Error GoTo ViewASSYPer_Err: 

    CloseDeleteSearchResultquery cstrQueryName 

    Set db = CurrentDb 

    'rebuild query 

    sqlStr = "SELECT ASSY_TABLE.assy_number, ASSY_TABLE.revision, ASSY_TABLE.description," 
    sqlStr = sqlStr & " ASSY_TABLE.target_weight, assy_weight_table.assy_weight, assy_weight_table.assy_fs, assy_weight_table.assy_bl, assy_weight_table.assy_wl" 
    sqlStr = sqlStr & " FROM ASSY_TABLE INNER JOIN assy_weight_table ON ASSY_TABLE.assy_number = assy_weight_table.assy_number " 
    sqlStr = sqlStr & " WHERE (((ASSY_TABLE.CollectorTypeCode) = """ & TypeofCode & """));" 

    Set qdf = db.CreateQueryDef(cstrQueryName) 
    qdf.SQL = sqlStr 

qdf.Fields(0).Properties("Caption").Value = "Assembly" 
qdf.Fields(1).Properties("Caption").Value = "Rev:" 
qdf.Fields(2).Properties("Caption").Value = "Description" 
qdf.Fields(3).Properties("Caption").Value = "Target Weight" 
qdf.Fields(4).Properties("Caption").Value = "Total Weight:" 
qdf.Fields(5).Properties("Caption").Value = "FS Arm" 
qdf.Fields(6).Properties("Caption").Value = "BL Arm" 
qdf.Fields(7).Properties("Caption").Value = "WL Arm" 

qdf.Fields(5).Properties("Format") = "Fixed" 
qdf.Fields(5).Properties("DecimalPlaces") = 3 

qdf.Fields(6).Properties("Format") = "Fixed" 
qdf.Fields(6).Properties("DecimalPlaces") = 3 

qdf.Fields(7).Properties("Format") = "Fixed" 
qdf.Fields(7).Properties("DecimalPlaces") = 3 

    DoCmd.OpenQuery cstrQueryName, acViewNormal, acReadOnly 

    Screen.ActiveDatasheet.AutoResize = True 

    Screen.ActiveDatasheet.Controls(0).SetFocus 
    DoCmd.RunCommand acCmdFreezeColumn 

    Set qdf = Nothing 
    Set db = Nothing 

    Exit Sub 


ViewASSYPer_Err_Exit: 
    Exit Sub 

ViewASSYPer_Err: 
    MsgBox Error$ 
    Resume ViewASSYPer_Err_Exit 

End Sub 
+0

更多信息来自Querydef解决方案提案。使用局部变量窗口,我查看了每个项目下的8个项目属性。计数属性确实有所不同。对于字段索引0到2,有35个属性。我在项目26中找到了标题属性。对于其他文件,有32个属性,但没有标题属性。代码失败的地方。有人知道为什么物业的数量可能会有所不同吗? –