AliciaMarie
New member
- Local time
- Today, 08:51
- Joined
- Nov 10, 2014
- Messages
- 5
I'm trying to add columns to a macro, and in the design view everything looks correct, but I'm getting an Error #6: Overflow. I added the columns "PercentExposureMiss", "PercentChoiceMiss" and "Percent Correct". It was working fine before these additions. Does anyone see a problem with the code? Thank you for your help!!
Public Function dmtsResultsExport()
Dim cantab As DAO.Database
Dim rstSession As DAO.Recordset
Dim strSQL As String
Dim myFileSystemObject As Object
Dim ExportFile As Object
Dim filenameDtTm As String
Dim vSubject As String
Dim vMemoryDelayMS, vDelayUsed, vNumCorrect, vNumIncorrect, vChoiceMissActual, vNumExposureMiss As String
Dim vPercentChoiceMiss, vPercentExposureMiss, vPercentCorrect, vAvgChoiceLatencyIncorrect, vAvgChoiceLatencyCorrect, vAvgSampleLatency As String
Dim LoopCount As Integer
Dim CurrentSubject As String
Dim MaxSessionCount As Integer
Dim DummyResponse As String
On Error GoTo ErrorHandler
Set cantab = CurrentDb '' Associate the cursor channel with the current DB (this one)
''
''
'' Find the Maximum number of sessions across all animals.
'' Write out to the text file that many heading groups
''
''
strSQL = "select max(spa) as headingcount from (select count(subject) as spa from cnprc_dmts_export_query group by subject)"
Set rstSession = cantab.OpenRecordset(strSQL, dbOpenDynaset)
rstSession.MoveFirst
With rstSession
If IsNull(![HeadingCount]) Then
DummyResponse = MsgBox("DMTS Export: No data found", vbOKOnly, "CANTAB")
rstSession.Close
cantab.Close
Set rstSession = Nothing
Set cantab = Nothing
Exit Function
End If
MaxSessionCount = ![HeadingCount]
End With
rstSession.Close '' Close the database cursor so we can use it again for reading the data to export
Set rstSession = Nothing
''
''
'' Set the export filename and open as a text file for writing
''
''
filenameDtTm = "C:\CANTAB Exports\DMTS " & Format$(Now(), "yyyy-mm-dd hh-mm") & ".csv"
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set ExportFile = myFileSystemObject.CreateTextFile(filenameDtTm, True)
DummyResponse = SysCmd(acSysCmdSetStatus, "Exporting to " & filenameDtTm) '' Set the status bar
ExportFile.write "Subject," '' The csv export file header has one line,
'' which begins with one occurance of the string "Subject"
For LoopCount = 1 To MaxSessionCount
ExportFile.write " DelayUsed," & "S-" & (LoopCount) '' Following Subject are repeating groups of the data headings
ExportFile.write " MemoryDelayMS," & "S-" & (LoopCount)
ExportFile.write " NumCorrect," & "S-" & (LoopCount) '' Each ends with S-n, where n is the session number
ExportFile.write " NumIncorrect," & "S-" & (LoopCount) '' For example FalseAlarms S-24
ExportFile.write " ChoiceMissActual," & "S-" & (LoopCount)
ExportFile.write " NumExposureMiss," & "S-" & (LoopCount)
ExportFile.write " PercentChoiceMiss," & "S-" & (LoopCount)
ExportFile.write " PercentExposureMiss," & "S-" & (LoopCount)
ExportFile.write " PercentCorrect," & "S-" & (LoopCount)
ExportFile.write " AvgChoiceLatencyIncorrect," & "S-" & (LoopCount)
ExportFile.write " AvgChoiceLatencyCorrect," & "S-" & (LoopCount)
ExportFile.write " AvgSampleLatency," & "S-" & (LoopCount)
Next LoopCount
ExportFile.WriteLine
''
''
'' Set the SQL statement again
'' Open the cantab cursor (rstSession) to read the CPT data for export
''
'' This data comes from a view created to provide the export records to
'' both this procedure and so that the users can easily see what the export
'' data actually is. This gives them an easy way to check on values that
'' look questionable.
''
''
strSQL = "SELECT Subject, DateTimeCode, DelayUsed, MemoryDelayMS, NumCorrect, NumIncorrect, ChoiceMissActual, NumExposureMiss, " & _
"PercentChoiceMiss, PercentExposureMiss, PercentCorrect, AvgChoiceLatencyIncorrect, AvgChoiceLatencyCorrect, AvgSampleLatency " & _
"FROM CNPRC_DMTS_Export_Query " & _
"ORDER BY Subject, DateTimeCode"
Set rstSession = cantab.OpenRecordset(strSQL, dbOpenDynaset)
'' If the recordset is empty, exit.
If rstSession.EOF Then
DummyResponse = SysCmd(acSysCmdClearStatus) ' Clears StatusBar
DummyResponse = MsgBox("DMTS Export: No data found", vbOKOnly, "CANTAB")
rstSession.Close
cantab.Close
ExportFile.Close
Set rstSession = Nothing
Set cantab = Nothing
Set ExportFile = Nothing
Exit Function
End If
rstSession.MoveFirst
LoopCount = 0
CurrentSubject = "NoSubject" '' Set an initial value so we dont end up with a blank line
With rstSession
Do Until .EOF Or LoopCount > 200000 '' Provide a method for the code to stop in case something is weird
LoopCount = LoopCount + 1 '' with the database or export view
vSubject = ![Subject]
vDelayUsed = ![DelayUsed]
vMemoryDelayMS = ![MemoryDelayMS]
vNumCorrect = ![NumCorrect]
vNumIncorrect = ![NumIncorrect]
vChoiceMissActual = ![ChoiceMissActual]
vNumExposureMiss = ![NumExposureMiss]
vPercentChoiceMiss = ![PercentChoiceMiss]
vPercentExposureMiss = ![PercentExposureMiss]
vPercentCorrect = ![PercentCorrect]
vAvgChoiceLatencyIncorrect = ![AvgChoiceLatencyIncorrect]
vAvgChoiceLatencyCorrect = ![avgchoicelatencycorrect]
vAvgSampleLatency = ![AvgSampleLatency]
If vSubject <> CurrentSubject Then
If CurrentSubject <> "NoSubject" Then '' Unless this is the first time through the loop,
ExportFile.WriteLine '' go to the next line
End If
CurrentSubject = vSubject
ExportFile.write (vSubject) & ","
End If
ExportFile.write (vDelayUsed) & "," '' Write out all the data values, separated by commas (csv!)
ExportFile.write (vMemoryDelayMS) & ","
ExportFile.write (vNumCorrect) & ","
ExportFile.write (vNumIncorrect) & ","
ExportFile.write (vChoiceMissActual) & ","
ExportFile.write (vNumExposureMiss) & ","
ExportFile.write (vPercentChoiceMiss) & ","
ExportFile.write (vPercentExposureMiss) & ","
ExportFile.write (vPercentCorrect) & ","
ExportFile.write (vAvgChoiceLatencyIncorrect) & ","
ExportFile.write (vAvgChoiceLatencyCorrect) & ","
ExportFile.write (vAvgSampleLatency) & ","
rstSession.MoveNext
Loop
End With
''
''
'' Close up the cursor, database channel and the text file
''
''
rstSession.Close
cantab.Close
ExportFile.Close
Set rstSession = Nothing
Set cantab = Nothing
Set ExportFile = Nothing
''
''
'' Display a message to the user with the export filename and number of sessions written
''
''
DummyResponse = SysCmd(acSysCmdClearStatus) ' Clears StatusBar
DummyResponse = MsgBox("Export file: " & filenameDtTm & Chr(13) & Chr(13) & _
" Total session exported: " & LoopCount & Chr(13) & _
" Maximum session number: " & MaxSessionCount, vbOKOnly, "CANTAB")
dmtsResultsExport = 0 '' Only needed because Access can call this routine only as a function
Exit Function
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function
Public Function dmtsResultsExport()
Dim cantab As DAO.Database
Dim rstSession As DAO.Recordset
Dim strSQL As String
Dim myFileSystemObject As Object
Dim ExportFile As Object
Dim filenameDtTm As String
Dim vSubject As String
Dim vMemoryDelayMS, vDelayUsed, vNumCorrect, vNumIncorrect, vChoiceMissActual, vNumExposureMiss As String
Dim vPercentChoiceMiss, vPercentExposureMiss, vPercentCorrect, vAvgChoiceLatencyIncorrect, vAvgChoiceLatencyCorrect, vAvgSampleLatency As String
Dim LoopCount As Integer
Dim CurrentSubject As String
Dim MaxSessionCount As Integer
Dim DummyResponse As String
On Error GoTo ErrorHandler
Set cantab = CurrentDb '' Associate the cursor channel with the current DB (this one)
''
''
'' Find the Maximum number of sessions across all animals.
'' Write out to the text file that many heading groups
''
''
strSQL = "select max(spa) as headingcount from (select count(subject) as spa from cnprc_dmts_export_query group by subject)"
Set rstSession = cantab.OpenRecordset(strSQL, dbOpenDynaset)
rstSession.MoveFirst
With rstSession
If IsNull(![HeadingCount]) Then
DummyResponse = MsgBox("DMTS Export: No data found", vbOKOnly, "CANTAB")
rstSession.Close
cantab.Close
Set rstSession = Nothing
Set cantab = Nothing
Exit Function
End If
MaxSessionCount = ![HeadingCount]
End With
rstSession.Close '' Close the database cursor so we can use it again for reading the data to export
Set rstSession = Nothing
''
''
'' Set the export filename and open as a text file for writing
''
''
filenameDtTm = "C:\CANTAB Exports\DMTS " & Format$(Now(), "yyyy-mm-dd hh-mm") & ".csv"
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set ExportFile = myFileSystemObject.CreateTextFile(filenameDtTm, True)
DummyResponse = SysCmd(acSysCmdSetStatus, "Exporting to " & filenameDtTm) '' Set the status bar
ExportFile.write "Subject," '' The csv export file header has one line,
'' which begins with one occurance of the string "Subject"
For LoopCount = 1 To MaxSessionCount
ExportFile.write " DelayUsed," & "S-" & (LoopCount) '' Following Subject are repeating groups of the data headings
ExportFile.write " MemoryDelayMS," & "S-" & (LoopCount)
ExportFile.write " NumCorrect," & "S-" & (LoopCount) '' Each ends with S-n, where n is the session number
ExportFile.write " NumIncorrect," & "S-" & (LoopCount) '' For example FalseAlarms S-24
ExportFile.write " ChoiceMissActual," & "S-" & (LoopCount)
ExportFile.write " NumExposureMiss," & "S-" & (LoopCount)
ExportFile.write " PercentChoiceMiss," & "S-" & (LoopCount)
ExportFile.write " PercentExposureMiss," & "S-" & (LoopCount)
ExportFile.write " PercentCorrect," & "S-" & (LoopCount)
ExportFile.write " AvgChoiceLatencyIncorrect," & "S-" & (LoopCount)
ExportFile.write " AvgChoiceLatencyCorrect," & "S-" & (LoopCount)
ExportFile.write " AvgSampleLatency," & "S-" & (LoopCount)
Next LoopCount
ExportFile.WriteLine
''
''
'' Set the SQL statement again
'' Open the cantab cursor (rstSession) to read the CPT data for export
''
'' This data comes from a view created to provide the export records to
'' both this procedure and so that the users can easily see what the export
'' data actually is. This gives them an easy way to check on values that
'' look questionable.
''
''
strSQL = "SELECT Subject, DateTimeCode, DelayUsed, MemoryDelayMS, NumCorrect, NumIncorrect, ChoiceMissActual, NumExposureMiss, " & _
"PercentChoiceMiss, PercentExposureMiss, PercentCorrect, AvgChoiceLatencyIncorrect, AvgChoiceLatencyCorrect, AvgSampleLatency " & _
"FROM CNPRC_DMTS_Export_Query " & _
"ORDER BY Subject, DateTimeCode"
Set rstSession = cantab.OpenRecordset(strSQL, dbOpenDynaset)
'' If the recordset is empty, exit.
If rstSession.EOF Then
DummyResponse = SysCmd(acSysCmdClearStatus) ' Clears StatusBar
DummyResponse = MsgBox("DMTS Export: No data found", vbOKOnly, "CANTAB")
rstSession.Close
cantab.Close
ExportFile.Close
Set rstSession = Nothing
Set cantab = Nothing
Set ExportFile = Nothing
Exit Function
End If
rstSession.MoveFirst
LoopCount = 0
CurrentSubject = "NoSubject" '' Set an initial value so we dont end up with a blank line
With rstSession
Do Until .EOF Or LoopCount > 200000 '' Provide a method for the code to stop in case something is weird
LoopCount = LoopCount + 1 '' with the database or export view
vSubject = ![Subject]
vDelayUsed = ![DelayUsed]
vMemoryDelayMS = ![MemoryDelayMS]
vNumCorrect = ![NumCorrect]
vNumIncorrect = ![NumIncorrect]
vChoiceMissActual = ![ChoiceMissActual]
vNumExposureMiss = ![NumExposureMiss]
vPercentChoiceMiss = ![PercentChoiceMiss]
vPercentExposureMiss = ![PercentExposureMiss]
vPercentCorrect = ![PercentCorrect]
vAvgChoiceLatencyIncorrect = ![AvgChoiceLatencyIncorrect]
vAvgChoiceLatencyCorrect = ![avgchoicelatencycorrect]
vAvgSampleLatency = ![AvgSampleLatency]
If vSubject <> CurrentSubject Then
If CurrentSubject <> "NoSubject" Then '' Unless this is the first time through the loop,
ExportFile.WriteLine '' go to the next line
End If
CurrentSubject = vSubject
ExportFile.write (vSubject) & ","
End If
ExportFile.write (vDelayUsed) & "," '' Write out all the data values, separated by commas (csv!)
ExportFile.write (vMemoryDelayMS) & ","
ExportFile.write (vNumCorrect) & ","
ExportFile.write (vNumIncorrect) & ","
ExportFile.write (vChoiceMissActual) & ","
ExportFile.write (vNumExposureMiss) & ","
ExportFile.write (vPercentChoiceMiss) & ","
ExportFile.write (vPercentExposureMiss) & ","
ExportFile.write (vPercentCorrect) & ","
ExportFile.write (vAvgChoiceLatencyIncorrect) & ","
ExportFile.write (vAvgChoiceLatencyCorrect) & ","
ExportFile.write (vAvgSampleLatency) & ","
rstSession.MoveNext
Loop
End With
''
''
'' Close up the cursor, database channel and the text file
''
''
rstSession.Close
cantab.Close
ExportFile.Close
Set rstSession = Nothing
Set cantab = Nothing
Set ExportFile = Nothing
''
''
'' Display a message to the user with the export filename and number of sessions written
''
''
DummyResponse = SysCmd(acSysCmdClearStatus) ' Clears StatusBar
DummyResponse = MsgBox("Export file: " & filenameDtTm & Chr(13) & Chr(13) & _
" Total session exported: " & LoopCount & Chr(13) & _
" Maximum session number: " & MaxSessionCount, vbOKOnly, "CANTAB")
dmtsResultsExport = 0 '' Only needed because Access can call this routine only as a function
Exit Function
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function