A small point of clarification: I am not accessing dxdatabook. I am going to the database behind dxdatabook and accessing that directly. In essence I am doing something similar to what dxdatabook does - reading from the database and doing something with the data. You should be able to do this from any script, so doing it from one that is connected to PADS and getting part information from there is equally possible as doing it from a script connected to DxDesigner.
There is a ton of information on the web about how to read/write data from a VB application or script to a database. The cool thing about the way it's done is that it can work with almost any kind of database. There are some basic building blocks you will need to become familiar with:
(note: this is done in vb.net. There will be differences if you are using vba in excel or vbscript, but the basic structure is similar. )
1. Connection string: this is a string of text that the built-in functionality of Windows and databases recognize as a command to establish a connection to the database. It will be represented by a string variable. It will be specific to the kind of database you are using. Here is the variable assignment for my connection string:
(in a public area of a module)
Public con_str AsString
Public my_db_path AsString
(in a subroutine)
my_db_path = "C:\my_db.accdb"
con_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & my_db_path
2. Connection object: This is part of the thing that handles all the behind the scenes API and system calls that Windows uses to get the work done between your application and the database. You don't need to worry too much about what is going on in there. Just create it and let it do its thing.
(in a public area of a module)
Public conn As OleDbConnection
(in a subroutine)
conn = New OleDbConnection(con_str)
The connection object makes use of the connection string to know what database to connect to. Then it needs a command to tell it to open the connection to the database. This is a simple command:
conn.Open()
Then you do more stuff with the open connection, which I'll get to in a moment. For now, just remember that whenever you open the connection, you need to also close it:
conn.Close
You should now be asking yourself how often you should open/close the database. Once for the entire application or for each time you need it? The answer is open and close it each time you need it. If left hanging open, invariably it will get stepped on by something and you'll get a crash.
3. Data adapter: The other part of the thing that Windows uses to interact with the database. Think of this and the connection as a handshake. The database on one side, Windows OS (and your application) on the other
4. Next there are three objects which are a hierarchy. Dataset, Datatable, Datarow. A dataset is a collection of data, which can be any of several things. It can be a group of tables. It can be a single table. It can be a subset of a table, or a single datarow. The easiest way to use it is by querying a table to get some data from it. While what you get in the dataset isn't a whole table, think of it as a small table made from a larger one. And think of it as a table. (Just be aware that it can be used many other ways too.)
So, in a simple scenario, a database is queried and a dataset is the result.. The dataset contains a table. The table contains rows. The rows are just like the rows of data in the database.
The tables in a dataset are indexed in a zero-based index. The rows in a table are indexed the same way, and the fields in a row are also indexed the same way.
5. Now how do you get the data you want from the database? SQL statements. There are other ways, but sql is the simplest and most powerful. It may seem daunting at first, but once you get a handle on how to use sql, you'll find that it's not complicated and pretty easy to use. The cool thing is that it works with many databases. So if by chance you change to a completely different database, as long as the tables in it are named the same and have the same column names, the sql statements don't need to change. The only thing you'd change is your connection string. Thus the use of sql is very portable.
I use sql as part of a function, so that much of the code only has to be written once. I have a bunch of such functions, that I apply as needed to run similar queries. Here is an example of such a function:
(conn, con_str, da, and sql are declared externally to the function)
PublicFunction select_from_table_where_field_equals_value(table AsString, field AsString, value AsString) As DataTable
conn = Nothing
conn = New OleDbConnection(con_str)
conn.Open()
'setup the sql statement
sql = "SELECT * FROM " & table & " WHERE " & field & " = " & Chr(34) & value & Chr(34) & ";"
'start the data adapter and load the table data
da = New OleDbDataAdapter(sql, conn)
Dim ds AsNew Data.DataSet
Dim dt AsNew Data.DataTable
Try
da.Fill(ds)
dt = ds.Tables(0)
Catch ex As Exception
'Debug.Print("failed with table: " & table & ", field: " & field & ", value: " & value)
EndTry
da = Nothing
ds = Nothing
dr = nothing
conn.Close
Return dt
dt = Nothing
EndFunction
Here are some more function calls and the sql statements in them:
PublicFunction select_from_table_where_id_is(table AsString, field AsString, value AsString) As Data.DataTable
sql = "SELECT * FROM " & table & " WHERE " & field & " = " & value & ";"
(this value will never have spaces, so no quote characters are needed)
PublicFunction select_from_table_with_two_values(table AsString, field1 AsString, value1 AsString, field2 AsString, value2 AsString) As Data.DataTable
sql = "SELECT * FROM " & table & " WHERE " & field1 & " = " & Chr(39) & value1 & Chr(39) & " AND " & field2 & " = " & Chr(39) & value2 & Chr(39) & ";"
(data may have spaces, quote characters are needed)
PublicFunction select_from_table_with_two_int_values(table AsString, field1 AsString, value1 AsInteger, field2 AsString, value2 AsInteger) As Data.DataTable
sql = "SELECT * FROM " & table & " WHERE " & field1 & " = " & value1 & " AND " & field2 & " = " & value2 & ";"
(integer values)
There is some very good information on how to construct sql statements here: http://www.w3schools.com/sql/default.asp
If you are using vbscript the methods are similar, and even simpler. There is a great example here, which bypasses the data adapter:
How to Connect to Access Database with VBScript
To do all this, you need to find out where your database is, get the correct connection string for it, and make sure you (and your users) have read access to it.