Declare Global type ' as NEW'

Hi all
i am Mario newbie on XOJO … older VB6 programmer … try to migrate from __> to XOJO,
my question
how can i declare a Global property type for access database

Dim db as NEW ODBCDatabase ??
im sorry for english
tnx

Hello,
select „Insert“ from the menu and insert a „Module“ into your project. You can name the Module however you like. Select the Module in the left pane of the IDE and again go to the „Insert“-menu and insert a „Property“. Name the property „db“ and set its type to ODBCDatabase. Also set the scope of the property to „Global“.

Somewhere in app.open you can do initialization:

DB = new ODBCDatabase

Christian Mzes tnx …
tried this solution in the first time but xojo dont permit me )necessary for me NEW asserction

Christian Schmitz
Try fast now im not at home

TNX to all

Make sure you have the ODBCPlugIn.xojo_plugin file in the Plugins folder. If it isn’t there, quit Xojo and copy it into the PlugIns folder from the Extras folder.

This is my open Event handler

 Dim db As New ODBCDatabase
  db.DataSource = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Aziende 2017\\ArcMag.mdb;UID=admin;PWD=;"
  
 If db.Connect then
    Sql = "SELECT * FROM AZIENDE ORDER BY Denominazione"
    AziRst = db.SQLSelect(sql)[/code]
End If

The problem is how can i add [quote]New ODBCDatabase[/quote] only 1 time for whole project ( NEW cant addedd)

If you put that code in App Open, it will execute only once.

I believe your problem is you are not properly understanding the entire declaration/scope/persistence matter:

If you declare the variable in an open event (in the app.open event, for example) that variable will only exist as long as that method (the open event) is active, and it will disappear as soon as the method ends.

To have a global variable that exists as long as the application does you need to have it in “some place” that will exist while your application does. That “place” is a module (as Christian Mzes suggested a few posts above, follow his instructions to create the variable). There are other possibilities to have global variables (it could be a property of a window, of the app itself), but a Module is the right option.

But creating a property in a module is not enough, you need to initialize it too. The initialization is done once and the app.open event would be a good place for it since that event runs only once. Your code in the app.open event should be (after you have created db as ODBCDatabase in a module, and set it as global):

[code] db=New ODBCDatabase
db.DataSource = “DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\Aziende 2017\ArcMag.mdb;UID=admin;PWD=;”

If db.Connect then
Sql = “SELECT * FROM AZIENDE ORDER BY Denominazione”
AziRst = db.SQLSelect(sql)[/code]

Julen

Many thanks to all
Solved!

In a module i create a property called db and declare DB as ODBCDatabase GLOBAL

In app.event handlers.Open wrote

db = NEW ODBCDatabase db.DataSource = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Aziende 2017\\ArcMag.mdb;UID=admin;PWD=;"

Happy job to all from Italy

Another problem
In my event handler\Open i have

  db = NEW ODBCDatabase
  Percorso =App.ExecutableFile.Parent.Parent.NativePath
  db.DataSource = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Percorso + "ArcMag.mdb;UID=;PWD=;"

db an Percorso are declared in a module a global
All correct on run but build nothing save without any error, why im sure error in these 3 lines or in the position ???

The paths are different between run and build.

#if DebugBuild then 
   Percorso =App.ExecutableFile.Parent.Parent.NativePath
#else
   Percorso =App.ExecutableFile.Parent.NativePath
#endif

Hi Tim thanks for reply

The problem is that it runs correctly in debug but when i go to build, it finish building without error messages … but does not create the executable.
If i remove this 3 lines for try it build.