VS2017 v15.6.7中的VB编码我试图从Access中的SQL查询结果创建一个文本文件。连接到数据库,填充表,SQL查询和 在datagridview上显示都可以。然后子程序(下面)导致此异常:
System.NullReferenceException:'对象变量或未设置块变量'
我是否正在尝试使用Access在VB中做到可行?
如果是这样,我会正确处理事情吗?
cellvalue
和spcfycel
的尺寸标注是否正确?
Private Sub Archivetxt()
' Archiving
Dim sep = ", " 'Declare separator
Dim filend = ".txt" 'Declare file extention
Dim logfile = "Test" & filend 'Construct log file name
Dim cellValue As VariantType
Dim i As Integer
Dim j As Integer
Dim spcfycel As Array 'what array required here
Dim OutputFile As System.IO.StreamWriter
OutputFile = New System.IO.StreamWriter("c:\#Log\" & logfile)
' Start Of double Loop
For i = 1 To LogManager.Count 'Number of rows returned in query
For j = 1 To 15 'Columns.Count '
cellValue = spcfycel(i, j).Value 'Write value of cell to variable cellValue
OutputFile.Write(cellValue) 'write in parameter
OutputFile.Write(sep) 'then separator
Next j
OutputFile.Write("(" & MainForm.monID & ")") 'add monitor's name
OutputFile.Write(OutputFile.NewLine) 'end line, start new line)
Next i
OutputFile.Close() 'Close the file
End Sub