Please can anyone help me.
I am trying to loop through 6 recordsets and write the results to a table which is used for another program that produces badge ID cards.
I am using Access 97 and would like to put the code as a module and call it from a button.
The problem I am having when I run my code is Data type conversion error. I think it could be the sql based recordsets. But I dont know how to get around this.
This is my Code.
Private Sub btncreatebgecrdtbl_Click()
Dim db As Database
Dim rsMaster As Recordset 'Main Table to write all to
Dim rsPeople As Recordset
Dim rsBTQual As Recordset
Dim rsNRSWAQual As Recordset
Dim rsBtEndorsment As Recordset
Dim rsOtherQuals As Recordset
Set db = CurrentDb
Set rsMaster = db.OpenRecordset("tblBadgeCrdsNew") 'must be just name of output table
Set rsPeople = db.OpenRecordset("SELECT tblBase.BaseName AS Depot," & _
"UCase([FirstName]) & ", " & UCase([Surname]) AS Employee_Name, tblEmployee.EmployeeID," & _
"tblEmployee.JobTitle, tblEmployee.MarconiID, tblBTLicence.BTLicenceID, tblEmployee.CSSID," & _
"tblBTLicence.IssueDate, tblBTLicence.ExpiryDate, tblEmpHistory.DateFinish" & _
"FROM (tblEmployee INNER JOIN tblBTLicence ON tblEmployee.EmployeeID = tblBTLicence.EmployeeID)" & _
"INNER JOIN (tblBase INNER JOIN tblEmpHistory ON tblBase.BaseID = tblEmpHistory.BaseID)" & _
"ON tblEmployee.EmployeeID = tblEmpHistory.EmployeeID" & _
"WHERE (((tblEmpHistory.DateFinish) Is Null")
Set rsBTQual = db.OpenRecordset("SELECT tblAchievement.EmployeeID, tblBTLicenceQual.QualCode " & _
"FROM tblQualification INNER JOIN (tblBTLicenceQual INNER JOIN tblAchievement" & _
"ON tblBTLicenceQual.QualID = tblAchievement.QualID) ON (tblQualification.QualID = tblAchievement.QualID)" & _
"AND (tblQualification.QualID = tblBTLicenceQual.QualID)" & _
"WHERE (((tblBTLicenceQual.Section) = 'BTLicence'" & _
"GROUP BY tblAchievement.EmployeeID, tblBTLicenceQual.QualCode")
Set rsNRSWAQual = db.OpenRecordset("SELECT tblAchievement.EmployeeID, tblBTLicenceQual.QualCode" & _
"FROM tblQualification INNER JOIN (tblBTLicenceQual INNER JOIN tblAchievement ON tblBTLicenceQual.QualID" & _
"= tblAchievement.QualID) ON (tblQualification.QualID = tblAchievement.QualID) AND (tblQualification.QualID" & _
"= tblBTLicenceQual.QualID)" & _
"WHERE (((tblBTLicenceQual.Section) = 'NRSWA'" & _
"GROUP BY tblAchievement.EmployeeID, tblBTLicenceQual.QualCode;")
Set rsOtherQuals = db.OpenRecordset("SELECT tblAchievement.EmployeeID, tblBTLicenceQual.QualCode" & _
"FROM tblQualification INNER JOIN (tblBTLicenceQual INNER JOIN tblAchievement ON tblBTLicenceQual.QualID" & _
"= tblAchievement.QualID) ON (tblQualification.QualID = tblAchievement.QualID) AND (tblQualification.QualID" & _
"= tblBTLicenceQual.QualID)" & _
"WHERE (((tblBTLicenceQual.Section) = 'other'" & _
"GROUP BY tblAchievement.EmployeeID, tblBTLicenceQual.QualCode;")
Set rsBtEndorsment = db.OpenRecordset("SELECT tblBTLicence.EmployeeID," & _
"[QualCode]" & " " & "[Date] AS Endorsements" & _
"FROM tblBTLicence INNER JOIN (tblBTLicenceQual INNER JOIN tblEndorsement" & _
"ON tblBTLicenceQual.QualID = tblEndorsement.QualID) ON tblBTLicence.BTLicenceID = tblEndorsement.BTLicenceID")
'Go to first record of Master loop to delete all existing records. Empty the existing recordset.
rsMaster.MoveFirst
Do Until rsMaster.EOF
rsMaster.Delete
Loop
rsPeople.MoveFirst
'This area collects all the data from the recordsets an populates the rsMaster recordset
Do Until rsPeople.EOF
rsMaster.AddNew
rsMaster!Depot = rsPeople!Depot
rsMaster!Employee_Name = rsPeople!Employee_Name
rsMaster!EmployeeID = rsPeople!EmployeeID
rsMaster!JobTitle = rsPeople!JobTitle
rsMaster!MarconiID = rsPeople!MarconiID
rsMaster!BTLicenceID = rsPeople!BTLicenceID
rsMaster!CSSID = rsPeople!CSSID
rsMaster!IssueDate = rsPeople!IssueDate
rsMaster!rsExpiryDate = rsPeople!ExpiryDate
rsMaster!rsDateFinish = rsPeople!DateFinish
rsMaster!Skillsets = rsBTQual!QualCode
rsMaster!NRWSA = rsNRSWAQual!QualCode
rsMaster!SpecialistSkills = rsOtherQuals!QualCode
rsMaster!Endorsements = rsBtEndorsment!Endorsements
'1st BT Qualifications
Dim strBTQual As String
Do Until rsBTQual.EOF
If rsBTQual!EmployeeID = rsPeople!EmployeeID Then
strBTQual = strBTQual & rsBTQual!QualCode & " "
End If
rsBTQual.MoveNext
Loop
rsMaster!BTQual = strBTQual
rsBTQual.MoveFirst
'2nd NRSWA Qualifications
Dim strNRSWAQual As String
Do Until rsNRSWAQual.EOF
If rsNRSWAQual!EmployeeID = rsPeople!EmployeeID Then
strNRSWAQual = strNRSWAQual & rsNRSWAQual!QualCode & " "
End If
rsNRSWAQual.MoveNext
Loop
rsMaster!BTQual = strNRSWAQual
rsNRSWAQual.MoveFirst
'3rd rsBtEndorsment
Dim strBtEndorsment As String
Do Until rsBtEndorsment.EOF
If rsBtEndorsment!EmployeeID = rsPeople!EmployeeID Then
strBtEndorsment = strBtEndorsment & rsBtEndorsment!Endorsements & " "
End If
rsBtEndorsment.MoveNext
Loop
rsMaster!BTQual = rsBtEndorsment
rsBtEndorsment.MoveFirst
'4th OtherQuals
Dim strOtherQuals As String
Do Until rsOtherQuals.EOF
If rsOtherQuals!EmployeeID = rsPeople!EmployeeID Then
strOtherQuals = strOtherQuals & rsOtherQuals!QualCode & " "
End If
rsOtherQuals.MoveNext
Loop
rsMaster!OtherQuals = strOtherQuals
rsOtherQuals.MoveFirst
'end of loops
rsPeople.MoveNext
Loop
rsMaster.Update
End Sub
I have also tried to define the sql as a string like this.
Private Sub Command0_Click()
Dim db As Database
Dim strsql As String
Dim rsMaster As Recordset 'Main Table to write all too
Dim rsPeople As Recordset
Dim qdf As QueryDef
Dim rsBTQual As Recordset
Dim rsNRSWAQual As Recordset
Dim rsBtEndorsment As Recordset
Dim rsOtherQuals As Recordset
db.Execute ("Create Table tblBadgeCrdsNew2" _
& "Depot text,Employee_Name text,EmployeeID text,JobTitle text,MarconiID text,BtLicenceID Int")
Set db = CurrentDb
' Set rsMaster = db.OpenRecordset("tblBadgeCrdsNew") 'must be just name of output table
Set rsMaster = db.OpenRecordset("tblBadgeCrdsNew2")
' Set rsPeople = db.OpenRecordset("SELECT * from tblEmployee")
strsql = "SELECT tblBase.BaseName AS Depot,"
strsql = strsql & "UCase([FirstName]) & ', ' & UCase([Surname]) AS Employee_Name, tblEmployee.EmployeeID,"
strsql = strsql & "tblEmployee.JobTitle, tblEmployee.MarconiID, tblBTLicence.BTLicenceID, tblEmployee.CSSID,"
strsql = strsql & "tblBTLicence.IssueDate, tblBTLicence.ExpiryDate, tblEmpHistory.DateFinish"
strsql = strsql & " FROM (tblEmployee INNER JOIN tblBTLicence ON tblEmployee.EmployeeID = tblBTLicence.EmployeeID)"
strsql = strsql & "INNER JOIN (tblBase INNER JOIN tblEmpHistory ON tblBase.BaseID = tblEmpHistory.BaseID)"
strsql = strsql & "ON tblEmployee.EmployeeID = tblEmpHistory.EmployeeID"
strsql = strsql & " WHERE (((tblEmpHistory.DateFinish)is null))"
Set rsPeople = db.OpenRecordset(strsql)
By refering setting the recordset to the string but I am not having much success.
Thank you.
regards,
Sid.
I am trying to loop through 6 recordsets and write the results to a table which is used for another program that produces badge ID cards.
I am using Access 97 and would like to put the code as a module and call it from a button.
The problem I am having when I run my code is Data type conversion error. I think it could be the sql based recordsets. But I dont know how to get around this.
This is my Code.
Private Sub btncreatebgecrdtbl_Click()
Dim db As Database
Dim rsMaster As Recordset 'Main Table to write all to
Dim rsPeople As Recordset
Dim rsBTQual As Recordset
Dim rsNRSWAQual As Recordset
Dim rsBtEndorsment As Recordset
Dim rsOtherQuals As Recordset
Set db = CurrentDb
Set rsMaster = db.OpenRecordset("tblBadgeCrdsNew") 'must be just name of output table
Set rsPeople = db.OpenRecordset("SELECT tblBase.BaseName AS Depot," & _
"UCase([FirstName]) & ", " & UCase([Surname]) AS Employee_Name, tblEmployee.EmployeeID," & _
"tblEmployee.JobTitle, tblEmployee.MarconiID, tblBTLicence.BTLicenceID, tblEmployee.CSSID," & _
"tblBTLicence.IssueDate, tblBTLicence.ExpiryDate, tblEmpHistory.DateFinish" & _
"FROM (tblEmployee INNER JOIN tblBTLicence ON tblEmployee.EmployeeID = tblBTLicence.EmployeeID)" & _
"INNER JOIN (tblBase INNER JOIN tblEmpHistory ON tblBase.BaseID = tblEmpHistory.BaseID)" & _
"ON tblEmployee.EmployeeID = tblEmpHistory.EmployeeID" & _
"WHERE (((tblEmpHistory.DateFinish) Is Null")
Set rsBTQual = db.OpenRecordset("SELECT tblAchievement.EmployeeID, tblBTLicenceQual.QualCode " & _
"FROM tblQualification INNER JOIN (tblBTLicenceQual INNER JOIN tblAchievement" & _
"ON tblBTLicenceQual.QualID = tblAchievement.QualID) ON (tblQualification.QualID = tblAchievement.QualID)" & _
"AND (tblQualification.QualID = tblBTLicenceQual.QualID)" & _
"WHERE (((tblBTLicenceQual.Section) = 'BTLicence'" & _
"GROUP BY tblAchievement.EmployeeID, tblBTLicenceQual.QualCode")
Set rsNRSWAQual = db.OpenRecordset("SELECT tblAchievement.EmployeeID, tblBTLicenceQual.QualCode" & _
"FROM tblQualification INNER JOIN (tblBTLicenceQual INNER JOIN tblAchievement ON tblBTLicenceQual.QualID" & _
"= tblAchievement.QualID) ON (tblQualification.QualID = tblAchievement.QualID) AND (tblQualification.QualID" & _
"= tblBTLicenceQual.QualID)" & _
"WHERE (((tblBTLicenceQual.Section) = 'NRSWA'" & _
"GROUP BY tblAchievement.EmployeeID, tblBTLicenceQual.QualCode;")
Set rsOtherQuals = db.OpenRecordset("SELECT tblAchievement.EmployeeID, tblBTLicenceQual.QualCode" & _
"FROM tblQualification INNER JOIN (tblBTLicenceQual INNER JOIN tblAchievement ON tblBTLicenceQual.QualID" & _
"= tblAchievement.QualID) ON (tblQualification.QualID = tblAchievement.QualID) AND (tblQualification.QualID" & _
"= tblBTLicenceQual.QualID)" & _
"WHERE (((tblBTLicenceQual.Section) = 'other'" & _
"GROUP BY tblAchievement.EmployeeID, tblBTLicenceQual.QualCode;")
Set rsBtEndorsment = db.OpenRecordset("SELECT tblBTLicence.EmployeeID," & _
"[QualCode]" & " " & "[Date] AS Endorsements" & _
"FROM tblBTLicence INNER JOIN (tblBTLicenceQual INNER JOIN tblEndorsement" & _
"ON tblBTLicenceQual.QualID = tblEndorsement.QualID) ON tblBTLicence.BTLicenceID = tblEndorsement.BTLicenceID")
'Go to first record of Master loop to delete all existing records. Empty the existing recordset.
rsMaster.MoveFirst
Do Until rsMaster.EOF
rsMaster.Delete
Loop
rsPeople.MoveFirst
'This area collects all the data from the recordsets an populates the rsMaster recordset
Do Until rsPeople.EOF
rsMaster.AddNew
rsMaster!Depot = rsPeople!Depot
rsMaster!Employee_Name = rsPeople!Employee_Name
rsMaster!EmployeeID = rsPeople!EmployeeID
rsMaster!JobTitle = rsPeople!JobTitle
rsMaster!MarconiID = rsPeople!MarconiID
rsMaster!BTLicenceID = rsPeople!BTLicenceID
rsMaster!CSSID = rsPeople!CSSID
rsMaster!IssueDate = rsPeople!IssueDate
rsMaster!rsExpiryDate = rsPeople!ExpiryDate
rsMaster!rsDateFinish = rsPeople!DateFinish
rsMaster!Skillsets = rsBTQual!QualCode
rsMaster!NRWSA = rsNRSWAQual!QualCode
rsMaster!SpecialistSkills = rsOtherQuals!QualCode
rsMaster!Endorsements = rsBtEndorsment!Endorsements
'1st BT Qualifications
Dim strBTQual As String
Do Until rsBTQual.EOF
If rsBTQual!EmployeeID = rsPeople!EmployeeID Then
strBTQual = strBTQual & rsBTQual!QualCode & " "
End If
rsBTQual.MoveNext
Loop
rsMaster!BTQual = strBTQual
rsBTQual.MoveFirst
'2nd NRSWA Qualifications
Dim strNRSWAQual As String
Do Until rsNRSWAQual.EOF
If rsNRSWAQual!EmployeeID = rsPeople!EmployeeID Then
strNRSWAQual = strNRSWAQual & rsNRSWAQual!QualCode & " "
End If
rsNRSWAQual.MoveNext
Loop
rsMaster!BTQual = strNRSWAQual
rsNRSWAQual.MoveFirst
'3rd rsBtEndorsment
Dim strBtEndorsment As String
Do Until rsBtEndorsment.EOF
If rsBtEndorsment!EmployeeID = rsPeople!EmployeeID Then
strBtEndorsment = strBtEndorsment & rsBtEndorsment!Endorsements & " "
End If
rsBtEndorsment.MoveNext
Loop
rsMaster!BTQual = rsBtEndorsment
rsBtEndorsment.MoveFirst
'4th OtherQuals
Dim strOtherQuals As String
Do Until rsOtherQuals.EOF
If rsOtherQuals!EmployeeID = rsPeople!EmployeeID Then
strOtherQuals = strOtherQuals & rsOtherQuals!QualCode & " "
End If
rsOtherQuals.MoveNext
Loop
rsMaster!OtherQuals = strOtherQuals
rsOtherQuals.MoveFirst
'end of loops
rsPeople.MoveNext
Loop
rsMaster.Update
End Sub
I have also tried to define the sql as a string like this.
Private Sub Command0_Click()
Dim db As Database
Dim strsql As String
Dim rsMaster As Recordset 'Main Table to write all too
Dim rsPeople As Recordset
Dim qdf As QueryDef
Dim rsBTQual As Recordset
Dim rsNRSWAQual As Recordset
Dim rsBtEndorsment As Recordset
Dim rsOtherQuals As Recordset
db.Execute ("Create Table tblBadgeCrdsNew2" _
& "Depot text,Employee_Name text,EmployeeID text,JobTitle text,MarconiID text,BtLicenceID Int")
Set db = CurrentDb
' Set rsMaster = db.OpenRecordset("tblBadgeCrdsNew") 'must be just name of output table
Set rsMaster = db.OpenRecordset("tblBadgeCrdsNew2")
' Set rsPeople = db.OpenRecordset("SELECT * from tblEmployee")
strsql = "SELECT tblBase.BaseName AS Depot,"
strsql = strsql & "UCase([FirstName]) & ', ' & UCase([Surname]) AS Employee_Name, tblEmployee.EmployeeID,"
strsql = strsql & "tblEmployee.JobTitle, tblEmployee.MarconiID, tblBTLicence.BTLicenceID, tblEmployee.CSSID,"
strsql = strsql & "tblBTLicence.IssueDate, tblBTLicence.ExpiryDate, tblEmpHistory.DateFinish"
strsql = strsql & " FROM (tblEmployee INNER JOIN tblBTLicence ON tblEmployee.EmployeeID = tblBTLicence.EmployeeID)"
strsql = strsql & "INNER JOIN (tblBase INNER JOIN tblEmpHistory ON tblBase.BaseID = tblEmpHistory.BaseID)"
strsql = strsql & "ON tblEmployee.EmployeeID = tblEmpHistory.EmployeeID"
strsql = strsql & " WHERE (((tblEmpHistory.DateFinish)is null))"
Set rsPeople = db.OpenRecordset(strsql)
By refering setting the recordset to the string but I am not having much success.
Thank you.
regards,
Sid.