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
Access database
-
- Posts: 2
- Joined: Nov 25, 2022 15:17
Re: Access database
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.
Reason: Added code tags.
-
- Posts: 1
- Joined: Apr 27, 2023 5:11
Re: Access database
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":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
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)
Last edited by fxm on Apr 27, 2023 7:36, edited 1 time in total.
Reason: Added code tags.
Reason: Added code tags.