0

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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
DayW
  • 11
  • 3
  • Arrays are initialized in VB as so `Dim spcfycel As Integer()` but maybe a list of string would be easier here. – Jacob H May 01 '18 at 20:11
  • "`Is the dimensioning of cellvalue and spcfycel correct?`" No, they are not. Modern VB pretty much **never** uses `Array` or `VariantType` in the `As` clause. You still have arrays, but that's not how to declare them. – Joel Coehoorn May 01 '18 at 20:18

0 Answers0