This is a discussion on Sybase to Excel within the Sybase forums, part of the Database Server Software category; --> I had a hard time finding this info scattered so I will post my code to help others. This ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I had a hard time finding this info scattered so I will post my code to help others. This is using a DSN connection named test_dsn set up going to Control Panel/Administration Tools/ODBC The ODBC settings were setup on the server where the sybase database was located. This was over a network so you may have to adjust for your situation. The ODBC connection is the key. Notice dbo.tablename in mySQL The UPDATE and INSERT have the sub name and end sub left off. I built most of the SQL's with variables so I had to cut them down for these examples. I think most will catch on. Actually, the IMPORT was changed to bring the values right to the spreadsheet so you will have to modify the code to do this. Good Luck wg ************************************************** ************* Public Sub ImportfromSYBASE_tablename() Dim cn As Object, rs As Object, myCalls As String Dim mySql As String, myCnt As Long 'later modify this SQL to get only needed records mySql = "SELECT * " & _ " FROM dbo.tablename ORDER BY tablename.columnName ASC" Set cn = New ADODB.Connection cn = "ODBC;Driver={SYBASE ASE ODBC Driver};DSN=test_dsn;SRVR=servername;DB=databasena me;UID=myUID;PWD=mypassword" cn.Open "ODBC;Driver={SYBASE ASE ODBC Driver};DSN=test_dsn;SRVR=servername;DB=databasena me;UID=myUID;PWD=mypassword" Set rs = New ADODB.Recordset With rs Set .ActiveConnection = cn .CursorLocation = adUseClient .Source = mySql 'Pass your SQL 'MsgBox mySql 'Turn on to see if getting correct SQL .Open mySql, cn, adOpenKeyset, adLockOptimistic, -1 myCnt = .RecordCount If myCnt > 0 Then .MoveLast: .MoveFirst 'Pull data to first sheet, cells a2:RecordestCountRow & column 7 Sheets("Sheet1").Select Range("A2").Select 'Delete any old data Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.ClearContents Range("A2").Select ActiveWorkbook.Worksheets("Sheet1").Range(Cells(2, 1), Cells(myCnt, 8)).CopyFromRecordset rs End If MsgBox (myCnt & " records were found!") Sheets("Sheet1").Select Range("A1").Select .Close End With cn.Close Set rs = Nothing: Set cn = Nothing End Sub ************************************************** ********* INSERT Dim cn As Object, rs As Object, myCalls As String Dim mySql As String, myCnt As Long 'INSERT INTO table_name (column1, column2,...) 'VALUES (value1, value2,....) Set cn = CreateObject("ADODB.Connection") cn.Open "FILEDSN="test_dsn";" & _ "Uid=myUID;" & _ "Pwd=mypass" Set rs = CreateObject("ADODB.Recordset") With rs Set .ActiveConnection = cn .Source = mySql 'Pass your SQL 'MsgBox mySql 'Turn on to see if getting correct SQL .Open mySql, cn, adOpenKeyset, adLockOptimistic, -1 'MsgBox (myCnt & " records were found!") Sheets("sheet2").Select Range("A1").Select .Close End With cn.Close Set rs = Nothing: Set cn = Nothing ************************************************ UPDATE Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long Set cn = New ADODB.Connection cn = "ODBC;Driver={SYBASE ASE ODBC Driver};DSN=test_dsn;SRVR=servername;DB=databasena me;UID=myUID;PWD=mypassword" cn.Open "ODBC;Driver={SYBASE ASE ODBC Driver};DSN=test_dsn;SRVR=servername;DB=databasena me;UID=myUID;PWD=mypassword" mySql = "Update dbo.tablename SET columnName = someText WHERE (tablename.columnName = someText)" Set rs = New ADODB.Recordset With rs Set .ActiveConnection = cn .CursorLocation = adUseClient .Source = mySql 'Pass your SQL 'MsgBox mySql 'Turn on to see if getting correct SQL .Open mySql, cn, adOpenKeyset, adLockOptimistic, -1 'myCnt = .RecordCount End With Set rs = Nothing cn.Close Set cn = Nothing MsgBox ("Database Updated !") Sheets("sheet2").Select Range("A1").Select |