Кодирование в VB в VS2017 v15.6.7 Я пытаюсь создать текстовый файл из результатов SQL-запроса в Access. Подключение к базе данных, заполнение таблицы, SQL-запрос и отображение на datagridview все в порядке. Затем подпрограмма (ниже) вызывает это исключение:
System.NullReferenceException: 'Object Variable or With block variable not set'
Возможно ли то, что я пытаюсь сделать в VB с Access?
Если да, то правильно ли я говорю?
Правильны ли размеры 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
"Is the dimensioning of cellvalue and spcfycel correct?
" Нет, это не так. Современный VB в значительной степени никогда использует Array
или VariantType
в пункте As
. У вас все еще есть массивы, но их нельзя объявлять.
Массивы инициализируются в VB как
Dim spcfycel As Integer()
, но, возможно, список строк здесь будет проще.