Coding in VB in VS2017 v15.6.7 I'm trying to create a text file from the results of an SQL query in Access. The connection to database, filling of table, SQL query and display on datagridview are all ok. Then the subroutine (below) causes this exception:
System.NullReferenceException: 'Object Variable or With block variable not set'
Is what I'm trying to do feasible in VB with Access?
If so am I going about things correctly?
Is the dimensioning of cellvalue
and spcfycel
correct?
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