1

无聊的背景,我正在尝试如何使用 Access 正确实现 VBA - 另一方面 - 希望在我的工作地点改进一个非常原始的 Access“应用程序”。管理是心态,如果“它”有效(无论多么糟糕),为什么要尝试改进“它”?

话虽如此,一个表单被用来显示要打开以进行编辑的表格列表。主窗体包含选择下拉框和最初链接到表格的子窗体。我正在尝试在子表单中打开选定的表,以便用户删除不需要处理的记录(这正在工作,这要归功于 stackoverflow.com)。

无论如何,用户选择表,然后创建 sql 语句。

选择 [PLS_Elkins_072513].[结构编号], [PLS_Elkins_072513].[结构注释], [PLS_Elkins_072513].[结构注释 2], [PLS_Elkins_072513].[结构注释 3], [PLS_Elkins_072513].[结构注释 4], [ PLS_Elkins_072513].[结构注释 5], [PLS_Elkins_072513].[结构注释 6], [PLS_Elkins_072513].[结构注释 7], [PLS_Elkins_072513].[结构注释 8], [PLS_Elkins_072513].[结构注释 9], [ PLS_Elkins_072513].[结构注释 10], [PLS_Elkins_072513].[结构注释 11], [PLS_Elkins_072513].[结构注释 12], [PLS_Elkins_072513].[结构注释 13], [PLS_Elkins_072513], [结构注释 1] PLS_Elkins_072513].[结构注释 15], [PLS_Elkins_072513].[结构注释 16], [PLS_Elkins_072513].[结构注释 17], [PLS_Elkins_072513].[结构注释 18], [PLS_Elkins_072513]。[结构注释 19], [PLS_Elkins_072513].[结构注释 20], [PLS_Elkins_072513].[结构注释 21], [PLS_Elkins_072513].[结构注释 22], [PLS_Elkins_072513].[结构注释 23], [PLS_Elkins_07251] [结构注释 24], [PLS_Elkins_072513].[结构注释 25], [PLS_Elkins_072513].[结构注释 26], [PLS_Elkins_072513].[结构注释 27], [PLS_Elkins_072513].[结构注释 28], [PLS_Elkins_07251] [结构注释 29], [PLS_Elkins_072513].[结构注释 30], [PLS_Elkins_072513].[结构注释 31], [PLS_Elkins_072513].[结构注释 32], [PLS_Elkins_072513].[结构注释 33], [PLS_Elkins_07251] [结构注释 34],[PLS_Elkins_072513]。[结构注释 35],[PLS_Elkins_072513]。[结构注释 36],[PLS_Elkins_072513]。[结构注释 37],[PLS_Elkins_072513]。[结构注释 38],[PLS_Elkins_072513].[结构注释 39], [PLS_Elkins_072513].[结构注释 40], [PLS_Elkins_072513].[结构注释 41], [PLS_Elkins_072513].[结构注释 42], [PLS_Elkins_072513], 注释 4 [PLS_Elkins_072513].[结构注释 44], [PLS_Elkins_072513].[结构注释 45], [PLS_Elkins_072513].[结构注释 46], [PLS_Elkins_072513].[结构注释 47], [PLS_Elkins_072513], 注释 4 [PLS_Elkins_072513].[结构注释 49],[PLS_Elkins_072513].[结构注释 50] 来自 PLS_Elkins_072513;[PLS_Elkins_072513].[结构注释 46], [PLS_Elkins_072513].[结构注释 47], [PLS_Elkins_072513].[结构注释 48], [PLS_Elkins_072513].[结构注释 49], [PLS_Elkins_072513]. [结构注释 5] PLS_Elkins_072513;[PLS_Elkins_072513].[结构注释 46], [PLS_Elkins_072513].[结构注释 47], [PLS_Elkins_072513].[结构注释 48], [PLS_Elkins_072513].[结构注释 49], [PLS_Elkins_072513]. [结构注释 5] PLS_Elkins_072513;

以下代码用于设置记录集

Me.sfrmTempTable.Form.RecordSource = strSQL

接下来我得到一个对话框,要求我输入 51 个字段的参数值……我按了“输入”51 次……

http://i138.photobucket.com/albums/q245/yosso22/Work%20Pictures/accessvba/enter_parameter_value_zps0f75f99d.png

VBA代码...

Private Sub EditStakingTable_Click()

Dim strSQL As String
Dim SelectTable As String

On Error GoTo Err_Handler

SelectTable = Me.InputTable.Value

' Check for a blank table name
If SelectTable = "" Then
    MsgBox "No Table Selected!!!", vbInformation, "Please select a table for editing."
    Exit Sub
End If

' Process - 6/13/13
' DoCmd.SetWarnings False ' Turn off warnings

strSQL = "SELECT [" & SelectTable & "].[Structure Number], [" & SelectTable & "].    [Structure Comment 1], [" & SelectTable & "].[Structure Comment 2], [" & SelectTable & "].    [Structure Comment 3], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 4], [" & SelectTable & "].    [Structure Comment 5], [" & SelectTable & "].[Structure Comment 6], [" & SelectTable & "].    [Structure  Comment 7], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 8], [" & SelectTable & "].    [Structure Comment 9], [" & SelectTable & "].[Structure Comment 10], [" & SelectTable & "].    [Structure Comment 11], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 12], [" & SelectTable & "].    [Structure Comment 13], [" & SelectTable & "].[Structure Comment 14], [" & SelectTable &     "].[Structure Comment 15], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 16], [" & SelectTable & "].[Structure Comment 17], [" & SelectTable & "].[Structure Comment 18], [" & SelectTable & "].[Structure Comment 19], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 20], [" & SelectTable & "].[Structure Comment 21], [" & SelectTable & "].[Structure Comment 22], [" & SelectTable & "].[Structure Comment 23], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 24], [" & SelectTable & "].[Structure Comment 25], [" & SelectTable & "].[Structure Comment 26], [" & SelectTable & "].[Structure Comment 27], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 28], [" & SelectTable & "].[Structure Comment 29], [" & SelectTable & "].[Structure Comment 30], [" & SelectTable & "].[Structure Comment 31], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 32], [" & SelectTable & "].[Structure Comment 33], [" & SelectTable & "].[Structure Comment 34], [" & SelectTable & "].[Structure Comment 35], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 36], [" & SelectTable & "].[Structure Comment 37], [" & SelectTable & "].[Structure Comment 38], [" & SelectTable & "].[Structure Comment 39], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 40], [" & SelectTable & "].[Structure Comment 41], [" & SelectTable & "].[Structure Comment 42], [" & SelectTable & "].[Structure Comment 43], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 44], [" & SelectTable & "].[Structure Comment 45], [" & SelectTable & "].[Structure Comment 46], [" & SelectTable & "].[Structure Comment 47], ["
strSQL = strSQL & SelectTable & "].[Structure Comment 48], [" & SelectTable & "].[Structure Comment 49], [" & SelectTable & "].[Structure Comment 50] FROM " & SelectTable & ";"

Debug.Print strSQL

Stop

' 8 / 09 / 13

Me.sfrmTempTable.Form.RecordSource = strSQL

' After tblStakingEdit has been opened, edited and closed
' copy (or rename) the tblStakingEdit table back to
' the originally named table

'DoCmd.SetWarnings True ' All done, turn on warnings back on.

Exit_Here:

Exit Sub

Err_Handler:
     If Err.Number = 2101 Then
     'Me.frmtesting.Visible = True
     'ignore or message
     Else
        MsgBox Err.Description
     End  If

     Resume Exit_Here

End Sub

代码运行后,我回到了原始屏幕,但子表单中的所有字段都显示“#Name?”。

最终产品的屏幕截图网址“ http://i138.photobucket.com/albums/q245/yosso22/Work%20Pictures/accessvba/final_screenshot_error_zpsa8ecb3a8.png

必须有一些我忽略的东西,因为我可以说是在“飞行”中学习。我确实购买了一本 MVP 书籍“访问解决方案”,其中有很多很好的例子,但现在我不知所措,正在向 stackoverflow.com 集体寻求帮助。:-)

4

1 回答 1

1

接下来我得到一个对话框,要求我输入 51 个字段的参数值……我按了“输入”51 次……

SELECT子句列出了 51 个字段。如果 中不存在这些字段PLS_Elkins_072513,则数据库引擎将假定(未找到的)字段名称实际上是参数,并要求您为它们提供值...... 51 次。然后子表单字段在表单的记录集中找不到它们的控制源,因此#Name?改为显示 Access。

您的 VBA 代码包括...

Debug.Print strSQL

因此,从即时窗口复制该输出,在查询设计器中创建一个新查询,切换到 SQL 视图,然后粘贴复制的文本。这种努力应该可以帮助您弄清楚您需要如何更改查询,以便 Access 将接受它而不要求提供参数。

从更大的角度来看,据我了解您描述的策略,它只有在所有可能的表选择都包含相同的 51 个字段时才有效。具有相同结构的多个表通常被认为是糟糕的设计。

在整理所有这些内容时,您可以为用户提供一种替代方法,以允许他们选择一个表并查看/编辑它。

DoCmd.OpenTable Me.InputTable.Value, acViewNormal

这并没有提供自定义表单的所有功能,但在您修复原始表单时,它是一个简单的临时组合。

于 2013-08-09T14:30:03.307 回答