AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ADO COM - Database Query
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Toxey



Joined: 13 Dec 2007
Posts: 31

PostPosted: Wed Mar 03, 2010 3:51 pm    Post subject: Reply with quote

I tried that twice, once within my program, and once just by itself, and got the same error (apparently I only got the other error once):

---------------------------
COM Error Notification
---------------------------
Function Name: "Open"
ERROR: (0x800A0E7F)
PROG:
DESC:
HELP: ,0

Will Continue?
---------------------------
Yes No
---------------------------

I'm going to try a few things, and I'll post if I have any success. Thanks, Sean.
Back to top
View user's profile Send private message Visit poster's website
Toxey



Joined: 13 Dec 2007
Posts: 31

PostPosted: Thu Mar 04, 2010 6:42 pm    Post subject: Reply with quote

I installed a Office 2000 on this machine (with Access) to see if it would solve any missing or corrupt file issues, and no luck

I also tried to run SFC, but not having the original physical 2000 disk on this dinosaur machine makes that difficult. I did manage to change the registry to look at a copy of the i386 folder from another machine, but no luck there either.

I'm going to let this fall back off my to-do list. I love a mystery, but not one without clues.
Back to top
View user's profile Send private message Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Fri Mar 05, 2010 12:17 am    Post subject: Reply with quote

My only suggestion remained is to try to re-install and/or update to the latest MDAC.
Back to top
View user's profile Send private message
The Unknown Jobber



Joined: 19 Nov 2009
Posts: 161
Location: Florida

PostPosted: Fri Jul 23, 2010 12:27 pm    Post subject: Re: Call stored procedure and get recordcount Reply with quote

HugeHugh wrote:
I have adapted namebran's solution to allow for getting the recordcount from the stored procedure result. You need to set the Cursorlocation to 3. This is working great:


Code:


#Include Lib\CoHelper.ahk
#Include Lib\Com.ahk

Com_CoInitialize()

idParam := "25"

sQuery := "MyStoredProcedureName"
sConnect := "Driver={SQL Server}; Server=servername; Database=dbname; UID=username; PWD=mypassword"
objcon := Com_ActiveXObject("ADODB.Connection")
objcmd := Com_ActiveXObject("ADODB.Command")

Com_Invoke(objcon, "CursorLocation", 3)   ; allows you to get RecordCount

Com_Invoke(objcon, "Open", sConnect)  ;Open
Com_Invoke(objcmd, "ActiveConnection", "+" . objcon)
Com_Invoke(objcmd, "CommandText", sQuery)
Com_Invoke(objcmd, "CommandType", 4)
Com_Invoke(objcmd, "NamedParameters", "True")

qParam1 := Com_Invoke(objcmd, "CreateParameter", "@idParam", 72, 1, 36, idParam)
objparam := Com_Invoke(objcmd, "Parameters")
Com_Invoke(objparam, "Append", "+" . qParam1)

spExec := Com_Invoke(objcmd, "Execute")
nRecordCount := Invoke(spExec, "RecordCount")

Loop, % nRecordCount
{
   pFields     := Invoke(spExec, "Fields")   ; Fields
   nFieldCount := Invoke(pFields, "Count")   ; Count
   sData       := ""

      Loop, %nFieldCount%
      {
          pField%A_Index% := Invoke_(pFields, "Item", 3, arg:=A_Index-1)      ; Item   
          sName  := Invoke(pField%A_Index%, "Name")   ; Name
          sValue := Invoke(pField%A_Index%, "Value")   ; Value
          sData .= sName . ":  " . sValue . "`r`n"
          Invoke(pField%A_Index%, "Release")           ; Release
      }

   MsgBox % sData

   Release(pFields)          ; Release
   Invoke(spExec, "MoveNext")  ; MoveNext
}


Com_Release(objcmd)
Com_Invoke(objcon, "Close")
Com_Release(objcon)
Com_CoUninitialize()
ExitApp



This code (and thread) helped me out... I slightly modified it though, mine uses LDAP syntax and I'm not getting a record count just grabbing certain fields that I need by name... there are some other slight differences too... but here's mine Smile and it works...

Code:


COM_init()
COM_CoInitialize()

lastName = Doe
firstName = Jane

connProv := "ADsDSOObject"
connOpen := "Active Directory Provider"
Query := "<LDAP://myldapserver>;(&(objectclass=user)(objectcategory=person)(&(sn=" lastName ")(givenName=" firstName ")));distinguishedname,cn,employeeid,mail,givenname,sn,middlename,samaccountname,displayname,department,division,manager;subtree"

oConn := COM_CreateObject("ADODB.Connection")
oComm := COM_CreateObject("ADODB.Command")

COM_Invoke(oConn, "Provider", connProv)
COM_Invoke(oConn, "Open", connOpen)

COM_Invoke(oComm, "ActiveConnection", "+" . oConn)
COM_Invoke(oComm, "Properties", "SearchScope", "2")
COM_Invoke(oComm, "Properties", "Page Size", "500")

COM_Invoke(oComm, "CommandText", Query)
adoExecute := COM_Invoke(oComm, "Execute")

Loop
{
   If COM_Invoke(adoExecute, "EOF")
      Break
     
   exFields := COM_Invoke(adoExecute, "Fields")
   exField := COM_Invoke(exFields, "Item", "cn")   
   result .= COM_Invoke(exField, "Name") . ": " . COM_Invoke(exField, "Value") . "`r`n"
   COM_Invoke(adoExecute, "MoveNext")
   COM_Release(exField)
}

MsgBox   %   result

COM_Invoke(oConn, "Close")
COM_Release(exFields)
COM_Release(exField)
COM_Release(oConn)
Back to top
View user's profile Send private message Visit poster's website
Toxey



Joined: 13 Dec 2007
Posts: 31

PostPosted: Mon Aug 30, 2010 11:13 pm    Post subject: Using ADO COM with dBase (dbf) files - Example Reply with quote

I'm not sure if anyone has posted this type of example, but I could not find it, so here goes. I hope this helps someone trying to solve the ADO riddle for dBase databases. Reading records and updating fields.

I cannot stress enough how much reading the msdn info on ADO helped me figure this out. http://msdn.microsoft.com/en-us/library/ms681025%28v=VS.85%29.aspx

I don't have an example dbf file to share, but assume that wrkorder.dbf contains a unique key field of WKORDNUM (work order number), a bunch of other fields, and two long string fields called NOTE1 and NOTE2 that are (not too surprisingly) for notes. varOurWkNum is just a work order number used to pick a specific record.

In my program, I grab the two notes, remove certain references, put them all at the end of the 2nd note and put an ETA date at the beginning of the 1st note. There's a lot of fancy formatting and frippery at work.

For simplicity, this example will just read the two fields, change them slightly, and write them back.


Code:
;Dbq is the path to the dbf file. (S:\ or whatever) This sConnect is highly dbf oriented (DBASE). (http://www.codeproject.com/KB/database/connectionstrings.aspx#dBASE)
sSource := "SELECT WKORDNUM,NOTE1,NOTE2 FROM " . "wrkorder.dbf" . " WHERE WKORDNUM = '" . varOurWkNum . "'"
sConnect := "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=S:\;"

COM_Init() ; (Initialize the process, start the motor, on your mark)
prs :=  COM_CreateObject("ADODB.Recordset") ; define prs as a Recordset object, ready to be acted upon

;Define properties of recordset that tell how it will operate (BEFORE recordset opened) (numbers from w3schools.com/ado/ado_ref_recordset.asp ).
COM_Invoke(prs, "CursorLocation=", 3) ; adUseClient = 3 , local (not on server) use of data
COM_Invoke(prs, "CursorType=", 0) ; adOpenForwardOnly = 0 , cursor will not go backward (I don't need to in this case)
COM_Invoke(prs, "LockType=", 3) ; adLockOptimistic = 3 , Not entirely clear on this, but won't lock out others entirely?

COM_Invoke(prs, "Open", sSource, sConnect) ; Open the recordset, if any, using the source and connect string from above.

Loop
{
   If  COM_Invoke(prs, "EOF")
      Break ; no fields? no match? (not sure) checking for end of fields
   pFields   := COM_Invoke(prs, "Fields") ; Get all the fields (together) in the current row of the recordset
   ; The below are out of order, just to prove it didn't matter. First field index is 0.
        ; You could loop through these, as in other examples, but to me this saves steps by defining the variables as they come.
   pField := COM_Invoke(pFields, "Item", 2) , varNote2 := COM_Invoke(pField, "Value") , COM_Release(pField)
   pField := COM_Invoke(pFields, "Item", 0) , varWorkyNum := COM_Invoke(pField, "Value") , COM_Release(pField)
   pField := COM_Invoke(pFields, "Item", 1) , varNote1 := COM_Invoke(pField, "Value") , COM_Release(pField)

   ;Add ETAXXXX in front of Note1 and NOETAXXXX at end of Note2
        varNote1 := "ETAXXXX" . varNote1
        varNote2 .= "NOETAXXXX"

   ;This does the actual value changing of the individual field in this record. 1st part defines/addresses, 2nd part changes, 3rd part releases.
   pField := COM_Invoke(pFields, "Item", 1) , COM_Invoke(pField, "Value=", varNote1) , COM_Release(pField)
   pField := COM_Invoke(pFields, "Item", 2) , COM_Invoke(pField, "Value=", varNote2) , COM_Release(pField)

   COM_Invoke(prs, "Update") ; Is this even necessary? Works without it I think. Anyone know?
   COM_Release(pFields) ; Release the recordset row (group of fields).
   COM_Invoke(prs, "MoveNext") ; Move to next row, if multirow
}
COM_Invoke(prs, "Close") ; Close the recordset
COM_Release(prs) ; Release the resources/memory (afaik)
COM_Term() ; Close the whole COM process (end to Init)


I hope this helps someone shortcut the chasm of ignorance I had to trudge over. If it doesn't make sense, steep yourself in ADO knowledge.

THANKS SEAN! I really don't think I can know how much I appreciate your COM work, and how excited I am that I finally understand enough to program direct access to my dbf files, rather than going through Access queries as I have been.

Thanks to all others for the contributed code examples.

Toxey
Back to top
View user's profile Send private message Visit poster's website
Toxey



Joined: 13 Dec 2007
Posts: 31

PostPosted: Tue Aug 31, 2010 1:56 pm    Post subject: Reply with quote

Side note to my last post.

Be careful with fields and dbf's that are indexed.

I don't believe we have any method of accessing and modifying NTX indexes on clipper databases through ADO COM, so I'm personally avoiding the problem.

Okay to modify unindexed fields (I hope), and don't delete or add an records. Basically, just sneaky modifications of "unwatched" fields.

So it's less useful than I first said, but it's still darn useful, and I will be on the lookout for a solution to the index problem.
Back to top
View user's profile Send private message Visit poster's website
captainspacedog



Joined: 21 Jul 2009
Posts: 27

PostPosted: Fri Sep 03, 2010 3:18 pm    Post subject: Ok. Example for writing to a Access DB. Reply with quote

I have adapted the examples in this post to pull data from Access Databases.

I am having a bit of trouble figuring out how to write data to a table.
Does anyone have an examples they have adapted, that they would be willing to share.

Thanks
Back to top
View user's profile Send private message
make it useful



Joined: 15 Sep 2010
Posts: 4

PostPosted: Wed Sep 15, 2010 1:48 am    Post subject: Setting cursor type when opening a recordeset with a comman Reply with quote

Hi

The following code attempts to run a stored procedure and work with the record set it returns.

Since i need to set timeout, and i will be adding parameters later, i need to use ADOB.Command.

Problem is that i cant seem to set the CursorType for the recordset and get it to stick.

Code:
sSource  := "sp_databases"
sConnect := "Provider=sqloledb;Data Source=MY-SERVER;Initial Catalog=MY-DATABASE;Integrated Security=SSPI;"


COM_Init()
prs :=   COM_CreateObject("ADODB.Recordset")
pcmd :=   COM_CreateObject("ADODB.Command")
COM_Invoke(pcmd, "CommandText", sSource)
COM_Invoke(pcmd, "CommandType", 4)
COM_Invoke(pcmd, "CommandTimeout", 400)
COM_Invoke(pcmd, "ActiveConnection", sConnect)

;;;;;;;;;;;  Method 1:  ;;;;;;;;;;;;

COM_Invoke(prs, "CursorType", 3)

activeLock := COM_Invoke(prs, "CursorType")
msgbox % activeLock

COM_Invoke(prs, "Open", "+" . pcmd)

activeLock := COM_Invoke(prs, "CursorType")
msgbox % activeLock

;Fails. The CursorType is reset when the connection is opened.

;;;;;;;;;;;  Method 2:  ;;;;;;;;;;;;

COM_Invoke(prs, "Open", "+" . pcmd, ,3)

;Fails with message: Error: Blank Parameter

;;;;;;;;;;;  Method 3:  ;;;;;;;;;;;;

COM_Invoke(prs, "Open", "+" . pcmd, sConnect, 3)

;Fails with message: Cannot change the ActiveConnection property of a Recordset object which has a Command object as its source.

COM_Invoke(prs, "Close")
COM_Release(prs)
COM_Term()


Thanks
Back to top
View user's profile Send private message
doyle



Joined: 14 Nov 2007
Posts: 325
Location: London, England

PostPosted: Wed Sep 22, 2010 1:21 pm    Post subject: Reply with quote

Any way to use the ADO functions to insert data into a database?

I have edited the function so the SQL is INSERT INTO, and not SELECT.

I get error code 0x80040E4D.

DESC: Authentication failed.
Back to top
View user's profile Send private message Visit poster's website
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Wed Sep 22, 2010 1:36 pm    Post subject: Reply with quote

then the problem is not the call its the credentials are either invalid or your credentials do not have insert privs for your table/database better get with your DBA
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
doyle2
Guest





PostPosted: Wed Sep 22, 2010 1:46 pm    Post subject: Reply with quote

God wrote:
then the problem is not the call its the credentials are either invalid or your credentials do not have insert privs for your table/database better get with your DBA


Im guessing it must be the INSERT priveleges, as I can SELECT fine.

I'm not sure how to grant priveleges to AHK. Its just MS Access accdb file.

I'll see if i can find out.
Back to top
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Wed Sep 22, 2010 4:21 pm    Post subject: Reply with quote

granting or revoking privilidges has nothing to do with ahk and must be done in the database by a user with priviledges to do so
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
doyle2
Guest





PostPosted: Thu Sep 23, 2010 7:29 am    Post subject: Reply with quote

I understand that.
Back to top
Rob T
Guest





PostPosted: Fri Feb 04, 2011 4:04 pm    Post subject: Reply with quote

I have this ADO function I use in VBA which executes SQL on the database.

Code:
db_file = [DBLOCATION].Value
    sql = [SQLSUBMIT].Value

    Set conn = New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & db_file & ";" & _
        "Persist Security Info=False"
    conn.Open

    'conn.Execute "DROP TABLE Employees"
    conn.Execute sql


Any idea how to specifically do the "conn.Execute" part in AHK?

I guess its full function is ADODB.Connection.Execute

Ive used Seans code above to retrieve data via select. however I have been unable to use it to CREATE/DROP/INSERT etc.
Back to top
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Fri Feb 04, 2011 4:12 pm    Post subject: Reply with quote

Executing a query is executing a query weather is select or create for drop the only difference is permissions expected return What error are you receive Are you sure you have permission to do what You need to do
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
Page 8 of 9

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group