Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

from Excel to Access

Status
Not open for further replies.

buddyel

MIS
Mar 3, 2002
279
US
Does anyone know how I can modify the following code so that before saving a new record, it will test for the existence of the text entered in Range("B8") in FIELD8 in the Access database, and if it does exist already, prompt the user to overwrite the record. Any suggestions are greatly appreciated..

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
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\engdata.mdb"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "tblEngdata", cn, adOpenKeyset, adLockOptimistic, adCmdTable
With rs
.AddNew 'creates a new record
'add values to each field in the record
.Fields("field8") = Range("b8").Value
.Fields("field9") = Range("b9").Value
.Fields("field10") = Range("b10").Value
.Fields("field11") = Range("b11").Value
.Fields("field12") = Range("b12").Value
.Fields("field13") = Range("b13").Value
.Fields("field14") = Range("b14").Value
.Fields("field15") = Range("b15").Value
.Fields("field16") = Range("b16").Value
.Fields("field17") = Range("b17").Value
.Fields("field18") = Range("b18").Value
.Fields("field19") = Range("b19").Value
.Fields("field20") = Range("b20").Value
.Fields("field21") = Range("b21").Value
.Fields("field22") = Range("b22").Value
.Fields("field23") = Range("b23").Value
.Fields("field24") = Range("b24").Value
.Fields("field25") = Range("b25").Value
.Fields("field26") = Range("b26").Value
.Fields("field27") = Range("b27").Value
.Update
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
 
Hi buddyel

I am assuming that Field8 is a unique value else if duplicates are allowed, and if some exist - which record do you overwrite.


So this code checks for existance - If exists already then prompts user to overwrite or cancel. If not exist already then addnew.


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

' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Development\Database\engdata.mdb"
' open a recordset
Set rs = New ADODB.Recordset


rs.Open "Select * from tblEngdata where [Field8] = '" & Range("B8").Value & "'", cn, 3, 3

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
For lngPos = 0 To 1
.Fields(lngPos).Value = "FIVE"
Next lngPos
.Update
Else
'Cancel
Exit Sub
End If
Else
.AddNew 'creates a new record
For lngPos = 8 To 27
.Fields(lngRow).Value = Cells(lngRow, 2).Value
Next lngPos
.Update
End If
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
 

Try this one instead - I had some code written in the "Overwrite" section of the last post that would allow me to test against a database on my PC. This post has it corrected to what you would want.

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=C:\Development\Database\engdata.mdb"
' open a recordset
Set rs = New ADODB.Recordset


rs.Open "Select * from tblEngdata where [Field8] = '" & Range("B8").Value & "'", cn, 3, 3

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
For lngPos = 8 To 27
.Fields(lngRow).Value = Cells(lngRow, 2).Value
Next lngPos
.Update
Else
'Cancel
Exit Sub
End If
Else
.AddNew 'creates a new record
For lngPos = 8 To 27
.Fields(lngRow).Value = Cells(lngRow, 2).Value
Next lngPos
.Update
End If
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
 
I now would like to add some values from column C. The cells are scattered (c5, c7, c14, c15, c44). How do i modify your for..next statement to add these in....
The fields will probably be named (fieldc5,fieldc7, etc..)

For lngPos = 8 To 27
.Fields(lngRow).Value = Cells(lngRow, 2).Value
Next lngPos
 
Hi Buddyel

The For .. Next statement in the last post worked out only because there was a correaltion between the cell rows and the fields and they all incremented by 1. If the cells are scattered in column C as you mentioned and there is no set increment then you may have to write them out for Column C as similar to what you had in your original post.

I am not sure if this will help but just a note about the For .. Next Loop

You can adjust the way it increments as follows

For lngRow = 2 to 10 Step 2
Msgbox LngRow
Next lngRow

The above code would return 2, 4, 6, 8 and then 10.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top