使用 SQL 将包含空字段的 MS Access 2016 表单导出到 Excel 电子表格



我正在将MS Access 2016表单数据导出到Excel电子表格。

Dim ctrlForm As Control
Dim sqlSelect As String
sqlSelect = "Select "
For Each ctrlForm  In Forms![Student Listing].Form.Controls
If TypeOf ctrlForm  Is TextBox Then
If ctrlForm .ColumnHidden = False Then
' this prints 109, which is text box - perfect
Debug.Print "Control Type: " & ctrlForm .ControlType
sqlSelect = sqlSelect & ctrlForm .Name & ","
End If 'end if for hidden              
End If 'end if for TypeOf
Next ctrlForm 
sqlSelect = Left(sqlSelect, Len(sqlSelect) - 1) 
sqlSelect = sqlSelect & " From " & Forms![Student Listing].Form.RecordSource
'prints the SQL statement - perfect too, all the view-able fields
Debug.Print "SQL Prompt: " & sqlSelect
Dim rs As Recordset
'errors here  with: Run-time error '3061' Too few parameters.
'Expected 1(or some other number) - even if there are 30 entries in the sqlSelect variable.
Set rs = CurrentDb.OpenRecordset(sqlSelect)

如果我隐藏所有可能包含"无数据 - 或空"的字段,它可以工作。 最大的问题是中间首字母,但我怀疑寻找空/空是关键。我该如何做到这一点并保留导出的Excel文件的空字段(列(?

">

参数太少"意味着您指定了一个字段名称,没有值(即使是空值 - 在这种情况下,字符串的数据部分没有足够的逗号(

选项 1:您唯一的错误可能是"ctrlForm .名称">

如果这不能解决它,请尝试:
测试控件文本是否为空 - 当它为空时,写出一个空字符串(或错误消息(和一个逗号,而不是跳过该字段。 所以调试后:

if .Name <> "" then
sqlSelect = sqlSelect & ctrlForm .Name & ","
else 
sqlSelect = sqlSelect & ","
end if

感谢您的所有帮助。我发现了问题。 问题原来是日期字段。 如果我不"导出"日期字段,一切正常。如果导出日期字段 - 我上面描述的错误消息。显然,这实际上是一个已知的事情。解决方法是创建一个包含日期字段文本的辅助"文本"字段。请改为导出该字段。 没有功能损失,但导出工作正常。

最新更新