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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP and ADO

Status
Not open for further replies.

VBFOXDEV35

Programmer
Mar 26, 2001
77
US
All, I am a little rusty when it comes to ADO and VFP. My question is, how do I get this code from VB to convert properly in VFP?

Any hints would help a lot. I am only stuck on how the connection strings should be set. As for the rest of ADO, I am all set. Thanks all!!!

Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
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=C:\temp\eclayton.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "loandata", cn, adOpenKeyset, adLockOptimistic, adCmdTable ' all records in a table
r = 2 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0 ' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("B" & r).Value
.Fields("FieldNameN") = Range("C" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

Art DeGaetano II
Software Developer, MOUS
 
Here is the code I use to create and return a connection. Maybe it will show you what you need.


LOCAL oConn as adodb.Connection
LOCAL cConnStr as Character

cConnStr = "provider=SQLOLEDB;" + ;
"Network Library=dbmssocn;" + ;
"user ID=" + this.sqlusername + ";" + ;
"password=" + this.sqluserpassword + ";" + ;
"Initial Catalog=" + this.sqldatabase + ";" + ;
"Data Source=" + this.sqlserver

oConn = createobject("adodb.connection")

oConn.CursorLocation = adUseClient
oConn.CommandTimeOut = 120
oConn.open(cConnStr)

RETURN oConn

 
Sorry I forgot a couple things. My code is written in VFP7 so if your not using 7 you may not be able to declare the connection as adodb.connection.

Also, VFP doesn't support the enums like VB so you have to enter a 3 instead of adUseClient.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top