Page 1 of 1 1
Topic Options
#177464 - 2007-07-03 06:07 AM use KIX to ACCESS database
bbs2me Offline
Lurker

Registered: 2007-06-28
Posts: 3
Hello,

I would like to use the kix to put some information to a access database.
I just copy the function DBExecuteSQL, DBConnOpen and DBConnClose that I found in UDF Library. But when I run it, I got an error: -2147352567.

Anyone can help me? Below is my source code for ref. Thank you.

 Code:
;FUNCTION DBExecuteSQL()
;
;ACTION Executes a SQL command on a database
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.11 (minor code changes)
; 1.1
;
;DATE CREATED 2002/09/05
;
;DATE MODIFIED 2004/03/07
;
;KIXTART 4.20+
;
;SYNTAX RETCODE = DBEXECUTESQL(CONNECTION, SQL [,CMDTYPE])
;
;PARAMETERS CONNECTION
; open connection object to a data source from DBConnOpen()
;
; SQL
; SQL command to be executed
;
; CMDTYPE
; Optional integer defining the command type (e.g. SQL statement, stored procedure)
;
;RETURN 0 if successful, otherwise error code
;
;REMARKS See also DBConnOpen(), DBConnClose(), DBRecordsetOpen(), DBRecordsetClose(), DBGetRecordset(), DBCommand()
;
; This routine does not return a recordset as the result of the SQL
; statement. Therefore, it should only be used with SQL commands
; like INSERT INTO, UPDATE. For SELECT statements one should rather
; use DBGetRecordset() or DBComand().
;
;DEPENDENCIES none
;
;EXAMPLE $objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=database.mdb')
; $retcode = DBExecuteSQL($objConn,"INSERT INTO Demo(Field1,Field2) VALUES('Value1','Value2')")
; $retcode = DBConnClose($objConn)
;
;KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82477

function DBExecuteSQL($objConn, $sql, optional $cmdType)
dim $cmdCommand, $rsRecordset
dim $adCmdUnspecified, $adCmdText, $adCmdTable, $adCmdStoredProc, $adCmdUnknown, $adCmdFile, $adCmdTableDirect
$adCmdUnspecified = -1
$adCmdText = 1 
$adCmdTable = 2 
$adCmdStoredProc = 4 
$adCmdUnknown = 8 
$adCmdFile = 256 
$adCmdTableDirect = 512 
$cmdType=iif(vartype($cmdType),val($cmdType),$adCmdText)
if vartype($objConn)<>9 or $sql=''
exit 87 
endif 
$cmdCommand = CreateObject('ADODB.Command')
if @ERROR
exit @ERROR 
endif 
$cmdCommand.ActiveConnection = $objConn
if @ERROR
exit @ERROR
endif
$cmdCommand.CommandType = $cmdType
if @ERROR
exit @ERROR
endif
$cmdCommand.CommandText = $sql
if @ERROR
exit @ERROR
endif $rsRecordset=$cmdCommand.Execute()
$rsRecordset=''
$cmdCommand=''
$DBExecuteSQL=@ERROR
exit @ERROR
endfunction





;FUNCTION DBConnOpen()
;
;ACTION Open a connection to a database using ADODB
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.11 (minor code changes)
; 1.1
;
;DATE CREATED 2002/09/05
;
;DATE MODIFIED 2004/03/07
;
;KIXTART 4.20+
;
;SYNTAX RETCODE = DBCONNOPEN(DSN [,CONNTIMEOUT, CMDTIMEOUT])
;
;PARAMETERS DSN
; Database connection string (ODBC format)
;
; CONNTIMEOUT
; Optional integer denoting the time in seconds until a connection times out, defaults to 15 seconds
;
; CMDTIMEOUT
; Optional integer denoting the time in seconds until a command times out, defaults to 30 seconds
;
;RETURN Connection object if successful, otherwise empty string
;
;REMARKS See also DBConnClose(), DBRecordsetOpen(), DBRecordsetClose(), DBGetRecordset(), DBCommand(), DBExecuteSQL()
;
; Example connection strings (requires appropriate ODBC drivers)::
;
; Microsoft Access : "DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=database.mdb"
; Microsoft SQL Server : "DRIVER={SQL Server};SERVER=servername;UID=user;PWD=password;DATABASE=mydatabase"
; Microsoft Visual FoxPro : "DRIVER={Microsoft Visual FoxPro Driver}; UID=; PWD=; DBQ=database.dbc"
; Oracle : "DSN=test;UID=username;PWD=password"
; For other connection strings please see http://www.connectionstrings.com
;
;DEPENDENCIES none
;
;EXAMPLE $objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=database.mdb')
;
;KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82470
;
Function DBConnOpen($ConnDSN, optional $ConnTimeout, optional $CmdTimeout)
Dim $objConn $ConnTimeout=iif(vartype($ConnTimeout),val($ConnTimeout),15)
$CmdTimeout=iif(vartype($CmdTimeout),val($CmdTimeout),30)
$ConnDSN=trim($ConnDSN)
if not $ConnDSN
exit 87
endif
$objConn = CreateObject("ADODB.Connection")
if @ERROR
exit @ERROR
endif
$objConn.ConnectionTimeout = $ConnTimeout
if @ERROR
exit @ERROR
endif
$objConn.CommandTimeout = $CmdTimeout
if @ERROR
exit @ERROR
endif
$objConn.Open($ConnDSN)
if @ERROR
exit @ERROR
endif
if not $objConn.State=1
$objConn=''
$DBConnOpen=''
exit @ERROR
endif
$DBConnOpen=$objConn
exit 0
EndFunction








;FUNCTION DBConnClose()
;
;ACTION Closes a connection to a database that has previously been opened with DBConnOpen()
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.1
;
;KIXTART 4.12+
;
;SYNTAX RETCODE = DBCONNCLOSE(CONNECTION)
;
;PARAMETERS CONNECTION
; open connection object to a data source from DBConnOpen()
;
;RETURN 0 if successful, otherwise error code
;
;REMARKS See also DBConnOpen(), DBRecordsetOpen(), DBRecordsetClose(), DBGetRecordset(), DBCommand(), DBExecuteSQL()
;
;DEPENDENCIES none
;
;EXAMPLE $retcode = DBConnClose($objConn)
;
;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic
;f=12
;t=000195
;
Function DBConnClose($objConn)
Dim $adStateOpen
$adStateOpen = 1
If vartype($objConn)=9
If $objConn.State = $adStateOpen
$objConn.Close()
if @ERROR
exit @ERROR
endif
EndIf
$objConn=''
else
exit 87
endif
$DBConnClose=0
exit 0
EndFunction



;======= M a i n =====================
$STATUS="login"

$objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=test.mdb')
$retcode = DBExecuteSQL($objConn,"INSERT INTO iplog(Date,Time,Site,PCName,LogonDomain,UserID,MACAddress,IPAddress,Status) VALUES(@Date,@Time,@Site,@Wksta,@LServer,@UserID,@Address,@IPaddress0,$STATUS)")
? $retcode
$retcode = DBConnClose($objConn)
? $retcode



Edited by Richard H. (2007-07-03 05:56 PM)
Edit Reason: Added code tags

Top
#177468 - 2007-07-03 09:31 AM Re: use KIX to ACCESS database [Re: bbs2me]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Please use the [ code ] [ /code ] tags (without spaces) for your code.
Top
#177483 - 2007-07-03 10:32 AM Re: use KIX to ACCESS database [Re: Arend_]
bbs2me Offline
Lurker

Registered: 2007-06-28
Posts: 3
What do you mean about use the [ code ] [ /code ] tags in my code. only the Main in write by me. The other function is found in UDF library.

Thank you.

Anyway, I have try to use the real data in the main program. But I still appear error.

 Code:
;=======   M     a      i      n   ============
$objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=test.mdb')
$retcode = DBExecuteSQL($objConn,"INSERT INTO iplog(Date,Time,Site,PCName,LogonDomain,UserID,MACAddress,IPAddress,Status) VALUES('2007/07/03','12:00:20','test','DEMO','\\DEMO','test','000E35B6E1FF','172.27.0.66','login')")
? $retcode
$retcode = DBConnClose($objConn)
? $retcode
;==================================



Edited by Richard H. (2007-07-03 05:54 PM)
Edit Reason: Added CODE tags

Top
#177484 - 2007-07-03 10:39 AM Re: use KIX to ACCESS database [Re: bbs2me]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
If you put the [ code ] above and [/ code ] below the code the formatting of the code is preserved and it is much easier to read. You should leave out the spaces in the code tags.

See KiXtart FAQ & How to's » The Post/Reply Formatting Box and How to use it for more info about the options when making a post.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#177547 - 2007-07-04 01:45 AM Re: use KIX to ACCESS database [Re: Mart]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
Try this... (make sure to update the path in $DB)

 Code:
;=======   M     a      i      n   ============
$DB="Path\test.mdb"
$objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=' + $DB)
? @serror
$retcode = DBExecuteSQL($objConn,"INSERT INTO iplog(Date,Time,Site,PCName,LogonDomain,UserID,MACAddress,IPAddress,Status) VALUES('2007/07/03','12:00:20','test','DEMO','\\DEMO','test','000E35B6E1FF','172.27.0.66','login')")
? $retcode
$retcode = DBConnClose($objConn)
? $retcode
;==================================
_________________________
(... better days ahead)

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 927 anonymous users online.
Newest Members
JimDunn, fgarcia, Viginette, ManuvdWielNL, Sir_Barrington
17890 Registered Users

Generated in 0.053 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org