Commands & Syntax > Commands > ODBC >

www.perfectkeyboard.com

 

OPEN - < odbc_open >() ... [Pro]

 

ODBC OPEN
<odbc_open>("Connection string",Variable for result)
Available in: Professional edition

This command opens a database using an ODBC driver. Note: The ODBC driver must be installed on the computer otherwise the command will fail. The Macro Toolworks (Perfect Keyboard) is a 32-bit application which requires a 32-bit ODBC driver.

 

#

Parameter name

Parameter description

1

Connection string

ODBC connection string. The connection string format depends on the type of the database. Get the format of the ODBC connection string from the database software provider or get the connection string formats from other sources, for example, https://www.connectionstrings.com
Example for Microsoft Access:
"Driver={MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)}; Dbq=c:\users\John Jimm\\documents\\MyDatabase.accdb;"

2

Variable for result

Variable that receives the handle of the opened database. This handle is then used in other ODBC commands as a parameter identifying the database.

 

Example (Macro Steps):

 

1

<#> <#> This example shows how to open and close a Microsoft Access database

2

Macro execution: ONLY COMMANDS

3

<#> <#> Show a form that lets a user to select the database file

4

Form FIELD "Microsoft Access database:" of type "File path" (Default value=*.accdb, Variable to save field value=vDbFile, Form identifier=f1)

5

Form OPEN "f1", Window title="Select Microsoft Access database to open"

6

IF %_vCanceled%==1

7

<#> <#> User canceled the selection form, no database file is selected

8

Macro EXIT

9

ENDIF

10

IF FILE "%vDbFile%" Not Exist ()

11

<#> <#> User entered a file that does not exist

12

Message SHOW "Error" : "The file '%vDbFile%' was not found." (other parameters: x = -100, y = -100, Window title = , Buttons = OK, Timeout (seconds) = 0, Always on top = No).

13

Macro EXIT

14

ENDIF

15

<#> <#> Build the Microsoft Access connection string

16

Variable SET "vConnectionString=Driver={MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)}; Dbq=%vDbFile%;", Message text=""

17

<#> <#> Open the database

18

ODBC OPEN Connection string=%vConnectionString%, Variable for result=vDbOpen

19

IF %vDbOpen%!=0

20

Message SHOW "Information" : "Database was sucessfully open." (other parameters: x = -100, y = -100, Window title = , Buttons = OK, Timeout (seconds) = 0, Always on top = No).

21

<#> <#> Close the database

22

ODBC CLOSE Database handle = %vDbOpen%

23

ENDIF

Example (Plain Text):

 

<#> This example shows how to open and close a Microsoft Access database

<cmds>

 

<#> Show a form that lets a user to select the database file

<form_item>("f1","Microsoft Access database:","EDIT_FILE","*.accdb","vDbFile",1)

<form_show>("f1","Select Microsoft Access database to open","",0,500,0,,,1,1)

 

<if>("%_vCanceled%==1")

   <#> User canceled the selection form, no database file is selected

   <exitmacro>

<endif>

 

<if_file>("%vDbFile%","NOTEXIST","")

   <#> User entered a file that does not exist

   <msg>(-100,-100,"The file '%vDbFile%' was not found.","",1,0,2,0)

   <exitmacro>

<endif>

 

<#> Build the Microsoft Access connection string

<varset>("vConnectionString=Driver={MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)}; Dbq=%vDbFile%;","")

 

<#> Open the database

<odbc_open>("%vConnectionString%",vDbOpen)

<if>("%vDbOpen%!=0")

   <msg>(-100,-100,"Database was sucessfully open.","",1,0,0,0)

  

   <#> Close the database

   <odbc_close>(%vDbOpen%)

<endif>