Access database

Windows specific questions.
Post Reply
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Access database

Post by phishguy »

Does anyone have an example on how to use Freebasic to enter data into a field on a Microsoft Access database? I pretty sure that Disphelper could do it, or possibly a ODBC wrapper. I have searched the forum and on the internet with no avail. The Disphelper site has a link for examples that is no longer working (it times out).

Thanks
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Post by dani.user »

On the front page of the website you can see a link to "FB ODBC Library" and a library for using activex controls
ercolesptr
Posts: 2
Joined: Nov 25, 2022 15:17

Re: Access database

Post by ercolesptr »

I managed to use DAO to access MS Access database with disphelper as follows:

Code: Select all

'#CONSOLE ON
#Define UNICODE
#Include Once "windows.bi"
#define UNICODE
#include "disphelper/disphelper.bi"
DISPATCH_OBJ(objDBEngine)
DISPATCH_OBJ(objWORKSpace)
DISPATCH_OBJ(objDATABase)
DISPATCH_OBJ(objRECORDSet)

dhInitialize(TRUE)
dhToggleExceptions(TRUE)
dhCreateObject("DAO.DBEngine.36", NULL, @objDBEngine)
dhGetValue("%o",@objWORKSpace,objDBEngine,".Workspaces(%d)",0)
dhGetValue("%o",@objDATABase,objWORKSpace,".OpenDatabase(%s)","C:\Test.mdb")
dhGetValue("%o",@objRECORDSet,objDATABase,".OpenRecordset(%s,dbOpenDynaset)","SELECT * FROM Details")

Dim As zstring Ptr szResponse
Dim as boolean bEOF

do
dhGetValue("%b",@bEOF,objRECORDSet,".EOF")
if (bEOF) then exit do
dhGetValue("%s", @szResponse, objRECORDSet, ".Fields(%s)","Name")
Print "Response: "; *szResponse
dhCallMethod(objRECORDSet, ".MoveNext")
loop
dhFreeString(szResponse)

SAFE_RELEASE(objRECORDSet)
SAFE_RELEASE(objDATABase)
SAFE_RELEASE(objWORKSpace)
SAFE_RELEASE(objDBEngine)
dhUninitialize(TRUE)
Print
Print "Press any key..."
Sleep
Last edited by fxm on Nov 30, 2022 19:09, edited 1 time in total.
Reason: Added code tags.
Lindaluttrell
Posts: 1
Joined: Apr 27, 2023 5:11

Re: Access database

Post by Lindaluttrell »

phishguy wrote: Sep 12, 2007 14:18 Does anyone have an example on how to use Freebasic to enter data into a field on a Microsoft Access database? I pretty sure that Disphelper could do it, or possibly a ODBC wrapper. I have searched the forum and on the internet with no avail. The Disphelper site has a link for examples that is no longer working (it times out).

Thanks
Yes, it is possible to enter data into a field on a Microsoft Access database using FreeBASIC. One way to accomplish this is by using an ODBC driver to connect to the database and execute SQL commands. Here's an example code snippet that demonstrates how to insert data into a table called "mytable" with columns "name" and "age":

Code: Select all

#include "vbcompat.bi"
#include "odbc.bi"

dim as integer henv, hdbc, hstmt
dim as string dsn = "your_datasource_name"
dim as string uid = "your_username"
dim as string pwd = "your_password"
dim as string query = "INSERT INTO mytable (name, age) VALUES ('John', 25)"

' Initialize the ODBC environment handle
if SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, @henv) <> SQL_SUCCESS then
    print "Error: Could not allocate environment handle"
    end
end if

' Set the ODBC environment attributes
SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, cast(SQLPOINTER, SQL_OV_ODBC3), SQL_IS_INTEGER)

' Allocate a connection handle
if SQLAllocHandle(SQL_HANDLE_DBC, henv, @hdbc) <> SQL_SUCCESS then
    print "Error: Could not allocate connection handle"
    end
end if

' Connect to the data source
if SQLConnect(hdbc, cast(SQLCHAR ptr, dsn), SQL_NTS, cast(SQLCHAR ptr, uid), SQL_NTS, cast(SQLCHAR ptr, pwd), SQL_NTS) <> SQL_SUCCESS then
    print "Error: Could not connect to data source"
    end
end if

' Allocate a statement handle
if SQLAllocHandle(SQL_HANDLE_STMT, hdbc, @hstmt) <> SQL_SUCCESS then
    print "Error: Could not allocate statement handle"
    end
end if

' Execute the SQL query
if SQLExecDirect(hstmt, cast(SQLCHAR ptr, query), SQL_NTS) <> SQL_SUCCESS then
    print "Error: Could not execute SQL query"
    end
end if

' Clean up
SQLFreeHandle(SQL_HANDLE_STMT, hstmt)
SQLDisconnect(hdbc)
SQLFreeHandle(SQL_HANDLE_DBC, hdbc)
SQLFreeHandle(SQL_HANDLE_ENV, henv)
Note that this code assumes that you have set up an ODBC data source for your Access database and that you have the appropriate driver installed. You will need to replace "your_datasource_name", "your_username", and "your_password" with the actual values for your system. Additionally, you should modify the query string to insert the actual data you want to add to the table.
Last edited by fxm on Apr 27, 2023 7:36, edited 1 time in total.
Reason: Added code tags.
Post Reply