Forums

This topic is locked

Convert a recordset to a variable

Posted 06 Oct 2010 20:56:33
1
has voted
06 Oct 2010 20:56:33 Thomas Lancaster posted:
Hello, I have an Excel macro which reads an Access database. I am trying to use two receordsets ("SS" & "SW" in an equaton and keep getting a type mismatch error. I think I need to convert the recordsets to variables "SampleSize" and "SampleWeight" first but I am at a lost as how to do this....Please help.

Public Sub DAOCopyFromRecordSet(DBFullName As String, TableName As String, _
FieldName As String, TargetRange As Range)

Dim db As Database, SW, SS, MC As Recordset
Dim intColIndex As Integer
Dim SampleSize, SampleWeight As Single ' These are variables to put recordset values in
Dim MatCode As String

Set TargetRange = TargetRange.Cells(1, 3)
Set db = OpenDatabase(DBFullName)

'SQL Query
Set SS = db.OpenRecordset("SELECT Std_Parts.Sample_Size FROM Std_Parts WHERE (((Std_Parts.[Part Number]) = '" & ActiveCell.Offset(0, 0).Value & "'))", dbReadOnly)
Set SW = db.OpenRecordset("SELECT Std_Parts.Sample_Weight FROM Std_Parts WHERE (((Std_Parts.[Part Number]) = '" & ActiveCell.Offset(0, 0).Value & "'))", dbReadOnly)
Set MC = db.OpenRecordset("SELECT Std_Parts.Material_Code FROM Std_Parts WHERE (((Std_Parts.[Part Number]) = '" & ActiveCell.Offset(0, 0).Value & "'))", dbReadOnly)

' write recordset
TargetRange.CopyFromRecordset SW / SS ' Getting type mismatch error here
Set TargetRange = TargetRange.Cells(1, 2)
TargetRange.CopyFromRecordset MC

ActiveCell.Offset(0, 6).Select
If ActiveCell.Value <> "" Then GoTo GoOn
ActiveCell.Formula = "=RC[-4]*RC[-1]"
GoOn:
ActiveCell.Offset(0, -6).Select


Set SW = Nothing
db.Close
Set db = Nothing

End Sub

Replies

Replied 04 Feb 2012 05:36:32
04 Feb 2012 05:36:32 Dino bikara replied:
Nice coding it is working. I tested.
Replied 15 Feb 2012 10:49:43
15 Feb 2012 10:49:43 anuiolow Simond replied:
Working!

Reply to this topic