I have questionaire data which is loaded from excel files which are in the format of one column for each question and one respondents responses in each row. In the database the questionaire data is stored in a normalised format i.e. TBL_RESPONSE has the following fields
IDResponse , QuestionID, AnswerID, AnswerText, JobID, BatchID
BatchID is generated by the database and each excel file loaded gets a batch ID
JobID links to the table of Jobs as the questionaires are QA on building jobs
JobID links to the table of Jobs as the questionaires are QA on building jobs
TBL_MonitoringQuestions has..
IDQuestion , QuestionCode, QuestionText, InChecker
QuestionCode is the actual heading of the column in the excel file
Inchecker is a boolean which flags whether the question is in the excel file (there are a couple of questions which are ignored by the database)
Inchecker is a boolean which flags whether the question is in the excel file (there are a couple of questions which are ignored by the database)
TBL_MonitoringAnswers has..
IDAnswer , Answer, Code
Again code is the actual text which is in the cell in the excel file when that answer has been given
TBL_MonitoringAllowedAnswers has..
IDQuestionAnswer , QuestionID, AnswerID
Hopefully that looks like a sensible format to store the data in. I decided on that after reading a lot of stuff about querstionaires on this site.
My question is on how to get the data from the excel style data in one question per column format to the database style linked table format.
Currently I am doing this. I have a query (well actually 2 queries) which lists all the valid question / answer combinations. I make a recordset of that query and then loop through the recordset loading all the yes's to question 1 then all the no's to question 1 then all the yes's to question 2 then all the no's to question 2 etc...
This method is really really slow! I'm sure there must be a better way that is staring me in the face. Please help.
Here is the first query I loop through the results of
SELECT TBL_MonitoringQuestions.QuestionCode, TBL_MonitoringQuestions.IDQuestion, TBL_MonitoringAnswers.IDAnswer, TBL_MonitoringAnswers.Code, TBL_MonitoringQuestions.InChecker
FROM TBL_MonitoringQuestions INNER JOIN (TBL_MonitoringAnswers INNER JOIN TBL_MonitoringAllowedAnswers ON TBL_MonitoringAnswers.IDAnswer = TBL_MonitoringAllowedAnswers.AnswerID) ON TBL_MonitoringQuestions.IDQuestion = TBL_MonitoringAllowedAnswers.QuestionID
WHERE (((TBL_MonitoringAnswers.Code) Is Not Null) AND ((TBL_MonitoringQuestions.InChecker)=True));
SELECT TBL_MonitoringQuestions.QuestionCode, TBL_MonitoringQuestions.IDQuestion, TBL_MonitoringAnswers.IDAnswer, TBL_MonitoringAnswers.Code, TBL_MonitoringQuestions.InChecker
FROM TBL_MonitoringQuestions INNER JOIN (TBL_MonitoringAnswers INNER JOIN TBL_MonitoringAllowedAnswers ON TBL_MonitoringAnswers.IDAnswer = TBL_MonitoringAllowedAnswers.AnswerID) ON TBL_MonitoringQuestions.IDQuestion = TBL_MonitoringAllowedAnswers.QuestionID
WHERE (((TBL_MonitoringAnswers.Code) Is Not Null) AND ((TBL_MonitoringQuestions.InChecker)=True));
and here is the second which deals with the free text questions
SELECT TBL_MonitoringQuestions.QuestionCode, TBL_MonitoringQuestions.IDQuestion, TBL_MonitoringAnswers.IDAnswer, TBL_MonitoringAnswers.Code, TBL_MonitoringQuestions.InChecker
FROM TBL_MonitoringQuestions INNER JOIN (TBL_MonitoringAnswers INNER JOIN TBL_MonitoringAllowedAnswers ON TBL_MonitoringAnswers.IDAnswer = TBL_MonitoringAllowedAnswers.AnswerID) ON TBL_MonitoringQuestions.IDQuestion = TBL_MonitoringAllowedAnswers.QuestionID
WHERE (((TBL_MonitoringAnswers.Code) Is Null) AND ((TBL_MonitoringQuestions.InChecker)=True));
FROM TBL_MonitoringQuestions INNER JOIN (TBL_MonitoringAnswers INNER JOIN TBL_MonitoringAllowedAnswers ON TBL_MonitoringAnswers.IDAnswer = TBL_MonitoringAllowedAnswers.AnswerID) ON TBL_MonitoringQuestions.IDQuestion = TBL_MonitoringAllowedAnswers.QuestionID
WHERE (((TBL_MonitoringAnswers.Code) Is Null) AND ((TBL_MonitoringQuestions.InChecker)=True));
The free text ones being the ones for which no allowable answer codes are given
Below now is my code that for making and looping through the recordset including the vb the insert statement that keeps putting results into TBL_MonitoringResponse
Public Sub append_results()
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.CursorType = adOpenKeyset
rst.LockType = adLockOptimistic
rst.Open "QRY_MonitoringAppendsToRun", CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.CursorType = adOpenKeyset
rst.LockType = adLockOptimistic
rst.Open "QRY_MonitoringAppendsToRun", CurrentProject.Connection
If rst.RecordCount > 0 Then
Do While Not rst.EOF
strQues = rst!QuestionCode
strID = rst!IDQuestion
strAnswer = rst!IDAnswer
strRead = rst!Code
Do While Not rst.EOF
strQues = rst!QuestionCode
strID = rst!IDQuestion
strAnswer = rst!IDAnswer
strRead = rst!Code
strSQL = "INSERT INTO TBL_MonitoringResponse ( MeasureID, QuestionID, AnswerID, BatchID ) " & _
"SELECT QRY_MonitorChecked.IDMeasure AS MeasureID, " & strID & " AS QuestionID, " & strAnswer & " AS AnswerID, " & intBatchID & " AS BatchID " & _
"FROM QRY_MonitorChecked " & _
"WHERE (((QRY_MonitorChecked." & strQues & ")='" & strRead & "'));"
CurrentProject.Connection.Execute strSQL
rst.MoveNext
Loop
Else
MsgBox "Couldn't locate questions table", vbOKOnly, "Fatal Error in Load"
Exit Sub
End If
"SELECT QRY_MonitorChecked.IDMeasure AS MeasureID, " & strID & " AS QuestionID, " & strAnswer & " AS AnswerID, " & intBatchID & " AS BatchID " & _
"FROM QRY_MonitorChecked " & _
"WHERE (((QRY_MonitorChecked." & strQues & ")='" & strRead & "'));"
CurrentProject.Connection.Execute strSQL
rst.MoveNext
Loop
Else
MsgBox "Couldn't locate questions table", vbOKOnly, "Fatal Error in Load"
Exit Sub
End If
rst.Close
rst.Open "QRY_MonitoringAppendsToRun2", CurrentProject.Connection
If rst.RecordCount > 0 Then
Do While Not rst.EOF
strQues = rst!QuestionCode
strID = rst!IDQuestion
strAnswer = rst!IDAnswer
Do While Not rst.EOF
strQues = rst!QuestionCode
strID = rst!IDQuestion
strAnswer = rst!IDAnswer
strSQL = "INSERT INTO TBL_MonitoringResponse ( MeasureID, QuestionID, AnswerID, BatchID, AnswerText ) " & _
"SELECT QRY_MonitorChecked.IDMeasure AS MeasureID, " & strID & " AS QuestionID, " & strAnswer & " AS AnswerID, " & intBatchID & " AS BatchID, QRY_MonitorChecked." & strQues & " AS AnswerText " & _
"FROM QRY_MonitorChecked;"
CurrentProject.Connection.Execute strSQL
rst.MoveNext
Loop
Else
MsgBox "Couldn't locate free questions table", vbOKOnly, "Fatal Error in Load"
Exit Sub
End If
"SELECT QRY_MonitorChecked.IDMeasure AS MeasureID, " & strID & " AS QuestionID, " & strAnswer & " AS AnswerID, " & intBatchID & " AS BatchID, QRY_MonitorChecked." & strQues & " AS AnswerText " & _
"FROM QRY_MonitorChecked;"
CurrentProject.Connection.Execute strSQL
rst.MoveNext
Loop
Else
MsgBox "Couldn't locate free questions table", vbOKOnly, "Fatal Error in Load"
Exit Sub
End If
rst.Close
MsgBox "Montitoring data has been loaded.", vbOKOnly, "Load Complete"
End Sub
rst.Close
MsgBox "Montitoring data has been loaded.", vbOKOnly, "Load Complete"
End Sub
rst.Close
rst.Open "QRY_MonitoringAppendsToRun2", CurrentProject.Connection
If rst.RecordCount > 0 Then
Do While Not rst.EOF
strQues = rst!QuestionCode
strID = rst!IDQuestion
strAnswer = rst!IDAnswer
Do While Not rst.EOF
strQues = rst!QuestionCode
strID = rst!IDQuestion
strAnswer = rst!IDAnswer
strSQL = "INSERT INTO TBL_MonitoringResponse ( MeasureID, QuestionID, AnswerID, BatchID, AnswerText ) " & _
"SELECT QRY_MonitorChecked.IDMeasure AS MeasureID, " & strID & " AS QuestionID, " & strAnswer & " AS AnswerID, " & intBatchID & " AS BatchID, QRY_MonitorChecked." & strQues & " AS AnswerText " & _
"FROM QRY_MonitorChecked;"
CurrentProject.Connection.Execute strSQL
rst.MoveNext
Loop
Else
MsgBox "Couldn't locate free questions table", vbOKOnly, "Fatal Error in Load"
Exit Sub
End If
"SELECT QRY_MonitorChecked.IDMeasure AS MeasureID, " & strID & " AS QuestionID, " & strAnswer & " AS AnswerID, " & intBatchID & " AS BatchID, QRY_MonitorChecked." & strQues & " AS AnswerText " & _
"FROM QRY_MonitorChecked;"
CurrentProject.Connection.Execute strSQL
rst.MoveNext
Loop
Else
MsgBox "Couldn't locate free questions table", vbOKOnly, "Fatal Error in Load"
Exit Sub
End If
rst.Close
MsgBox "Montitoring data has been loaded.", vbOKOnly, "Load Complete"
End Sub
MsgBox "Montitoring data has been loaded.", vbOKOnly, "Load Complete"
End Sub