The code below pulls the data from the database but it also pulls the field headings, along with all the records from the database. I cant seem to change the .Open "Select" statement to make it work right. The tablename is tblEngdata, the fieldname should be dwgno and the criteria should be Range("b8"
Sub ADOImportFromAccessTable(DBFullName As String, TableName As String, TargetRange As Range)
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
' open the recordset
.Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable ' all records
'.Open "SELECT * FROM " & TableName & " WHERE [FieldName] = 'MyCriteria'", cn, , , adCmdText ' filter records
RS2WS rs, TargetRange ' write data from the recordset to the worksheet
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Sub RS2WS(rs As ADODB.Recordset, TargetCell As Range)
Dim f As Integer, r As Long, c As Long
If rs Is Nothing Then Exit Sub
If rs.State <> adStateOpen Then Exit Sub
If TargetCell Is Nothing Then Exit Sub
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.StatusBar = "Writing data from recordset..."
End With
With TargetCell.Cells(1, 1)
r = .Row
c = .Column
End With
With TargetCell.Parent
.Range(.Cells(r, c), .Cells(.Rows.Count, c + rs.Fields.Count - 1)).Clear ' clear existing contents
' write column headers
For f = 0 To rs.Fields.Count - 1
On Error Resume Next
.Cells(r, c + f).Formula = rs.Fields(f).Name
On Error GoTo 0
Next f
' write records
On Error Resume Next
rs.MoveFirst
On Error GoTo 0
Do While Not rs.EOF
r = r + 1
For f = 0 To rs.Fields.Count - 1
On Error Resume Next
.Cells(r, c + f).Formula = rs.Fields(f).Value
On Error GoTo 0
Next f
rs.MoveNext
Loop
.Rows(TargetCell.Cells(1, 1).Row).Font.Bold = True
.Columns("A:IV"
End With
With Application
.StatusBar = False
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Sub StartHere()
ADOImportFromAccessTable "e:\engdata.mdb", "tblEngdata", Range("B8"
End Sub
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim ExistingRecord As Boolean
Dim prmptUser As Long
Dim lngPos As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\Development\Databases\engdata.mdb"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Select * from tblEngdata where [dwgno] = '" & Range("B8"
With rs
If .RecordCount > 0 Then
ExistingRecord = True
Else
ExistingRecord = False
End If
If ExistingRecord = True Then
prmptUser = MsgBox("Record already exists - Overwrite Record ?", vbYesNo + vbQuestion, "What to do ?"
If prmptUser = vbYes Then
'Overwrite
.Fields("dwgno"
.Fields("rev"
.Fields("rodno"
.Fields("rodno2"
.Fields("sl1"
.Fields("rodsperhtr"
.Fields("ceone1"
.Fields("cetwo1"
.Fields("addlinfo"
.Fields("strip1"
.Fields("strip2"
.Fields("elemwatts"
.Fields("elemvolts"
.Fields("ohms"
.Fields("amps"
.Fields("spcstampinfo"
.Fields("rodelong"
.Fields("pinelong"
.Fields("resisfactor"
.Fields("pinext"
.Fields("watttolp"
.Fields("watttolm"
.Fields("coldohmfactor"
.Fields("annealtype"
.Fields("note1"
.Fields("note2"
.Fields("note3"
.Fields("spcroddata"
.Fields("totalpower"
.Fields("ph"
.Fields("totalvolts"
.Fields("expterminfo"
.Fields("pinext"
.Fields("headpinlgth"
.Fields("tailpinlgth"
.Fields("rodratio"
.Fields("sl2"
.Fields("ceone2"
.Fields("cetwo2"
.Fields("dia"
.Update
Else
'Cancel
Exit Sub
End If
Else
.AddNew 'creates a new record
.Fields("dwgno"
.Fields("rev"
.Fields("elemwatts"
.Fields("elemvolts"
.Fields("ohms"
.Fields("amps"
.Fields("note1"
.Fields("note2"
.Fields("note3"
.Fields("spcroddata"
.Fields("totalpower"
.Fields("ph"
.Fields("totalvolts"
.Fields("pinext"
.Fields("headpinlgth"
.Fields("tailpinlgth"
.Fields("rodratio"
.Fields("sl2"
.Fields("ceone2"
.Fields("cetwo2"
.Fields("dia"
.Update
End If
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub