# Revision history [back]

Hello,

There are a few methods to connect to a database from Calc.

You have stated that you have a DB already registered. The connection to the base file is easy using the registered name.

Sub ConnectDB
Dim oContext As Object
Dim oDB        As Object
Dim oCon      As Object
oContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
oDB = oContext.getByName("Registered_Name_Here")
End Sub


oCon is the connection you can use to perform your SQL with such as:

  oStatement = oCon.CreateStatement()
sSQL = "SELECT NAME FROM MYTABLE"
oResult = oStatement.executeQuery(sSQL)
Do While oResult.next()
Print oResult.getString(1)
Loop


If you want to bypass the Base file and connect directly to the Database, then the basic directions can be found in the AndrewBase document found here -> Andrew Pitonyak Documents. Look at page 106 - 8.6.6. Paradox using ODBC. This was the basis for my answer on this post -> Connect to mssql (ODBC) database via Macro.