Accessing native DB from Excel

ta5ta5 Member Posts: 1,164
Hello
I have searched the forum, but did not found a solution...

I want to access 2009 SP1 database from excel 2007.
Function ConnectDB()
   Set objConn = New ADODB.Connection
  On Error GoTo ConnErr
  objConn.Mode = adModeRead
  objConn.ConnectionString = "DSN=xy"
  objConn.Open
   Exit Function
  ConnErr:
  MsgBox ("Error in connection!")
End Function

Function QueryDB(strSQL As String)
    On Error GoTo QueryErr
    Set objRs = New ADODB.Recordset
    objRs.Open strSQL, objConn
    objRs.MoveFirst
    MsgBox (objRs.RecordCount)
    'MsgBox (objRs.Fields(1).Value)
    Exit Function
    
QueryErr:
    MsgBox ("Error in query!")
End Function

Function DoIt()
 ConnectDB
 QueryDB ("Select * from Item")
End Function

Function DoIt is for testing purposes.
RecordCount returns -1, although the table is not empty. I also tried with "Select * from Artikel", which is the correct name in german, since I work on a swiss german windows.
Anybody sees the bug?

Thanks in advance
Thomas

Comments

  • ta5ta5 Member Posts: 1,164
    It works :) now:
    This is the correct code:
    Function ConnectDB()
      Set objConn = New ADODB.Connection
      On Error GoTo ConnErr
      objConn.Mode = adModeRead
      objConn.ConnectionString = ConnectStr("Test")
      objConn.CursorLocation = adUseClient
      objConn.Open
      Exit Function
    ConnErr:
      MsgBox ("Error in connection!")
    End Function
    

    Now I have still one problem:
    How to set the company via Code?
    This works as long as the company is set in the dsn (
    "RO=Yes;CC=Yes;BE=Yes;CD=Yes;ML=2055;RD=No;UID=myUID;PWD=1234;CN=Test;SERVER=N;"
    

    Any ideas?
    Thanks
    Thomas
Sign In or Register to comment.