Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

c c 

 c
  
O? Database ready to attach
O? Database software installed on a PC (optional)
O? îisual Basic Express installed on a PC (optional)
O? îisual Studio.NET installed on a PC (optional)

1.? 1

Open your developer environment, then open the project you're working on. If you don't
have a project yet, create one by clicking "Create" next to "Project." A new window
opens. The SQL query will work in any of the application types, but the easiest way is to
select "Windows Form Application". Type the name of your file in the "Name" box and
press "OK."

2.? 2

Navigate to the top menu bar and click on "Data." Choose "Add New Data Source."
Follow the screen prompts to add the database to your project. The box will close when
you're finished.

3.? 3

Click on "Data" again in the top menu choices. Choose "Add Query", and a new box will
open showing you the fields you created in your database. Click "Query Builder" at the
bottom. The query builder window will open.

4.? 4

Move the borders of the query-building window so you can fully see all the aspects
available to you. The top section shows your database fields. Unclick any fields you do
not want SQL query to sort. For example, if you need to find all cars made between the
years of 1976 and 1985, then unclick fields like "Motorcycles," "Trucks" or "Last Name"
because those fields are not relevant to the search.

5.? 5

Drop down to the next section to choose how you want the fields you are sorting to
display. Click "Output" if it is not already checked. Choose the "Sort Type" and "Sort
Order." The pane below shows the actual SQL query as it's created.

6.? 6
Click "Execute Query" to test it. Executing it is the same as commanding it to run, and
executing it effectually links the SQL query to your îisual Basic project. Click "OK"
when you're done.

Read more: How to Link SQL Queries With îisual Basic | eHow.com
http://www.ehow.com/how_6351860_sql-queries-visual-basic.html#ixzz1DWKDeFWw
?

î 
  
  

This is not about how to create or design a database, it is about how to connect to a database and
manipulate a database using îB. It will work (with some minor alterations) in îBA as well.

There are several ways of connecting to a database (for example, Access), via data bound
controls, DAO or ADO. On the whole I do not use data bound controls because I like to keep
control of what is happening to the data so the rest is about DAO/ADO.

To start with you need to create a few variables of the following types

O? £ ?
 ?????? ??? ? ???
? ??
O? !??? ? ??!?" ??
O? 
" ?????!#?$?$!??
O? º?
º??? ?? ?? ?! ????

’   


 

With an Access database it is possible to connect to the database in 2 ways, JET or ODBC.
Personally I use ODBC because the file management is easier; to change the filename or path
just use the ODBC administrator in the control panel.

Note: These examples are here to show what to do very simply, some of the commands have
more options than are shown so please review the help files for more details.

  ccc

Dim ws as Workspace
Dim db as Database

Set ws=DBEngine.Workspaces(0)
set db=ws.OpenDatabase({databasepath and name})

  ccc

Dim ws as Workspace
dim db as database
dim strConnection as string

set ws=DBEngine.Workspaces(0)
let strConnection= "ODBC;DSN=" & DatabaseName & ";UID=" & UserName
& ";PWD=" & UserPassword
set db=ws.OpenDatabase("", False, False, strConnection)

 

Dim ad as ADODB.Connection

set ad=New ADODB.Connection


Let ad.ConnectionString= "ODBC;DSN=" & DatabaseName & ";UID=" &
UserName & ";PWD=" & UserPassword
ad.Open

  
 

Now we have the database connection established it is time to look at the data. The following
example show how to open a table/query and move through it.

  

Dim rs as recordset

set rs=db.openrecordset({tablename or SQL})


do while not rs.eof
'Put the code here for what to do with the information.
'The field information can be access by the field name
intID=rs!IDField
'Or by the order number it is in the list (starting at 0)
intString=rs.Field(1)
rs.movenext
loop

 

dim ar as ADODB.recordset

set ar=new adodb.recordset


ar.open {SQL Statement}
do while not ar.EOF
'Put the code here for what to do with the information.
'The field information can be access by the field name
intID=ar!IDField
'Or by the order number it is in the list (starting at 0)
intString=ar.Field(1).value
ar.movenext
loop

’ 

To edit/add/delete a record we can do it either using SQL or directly. Both DAO and ADO use
the execute method for doing updates by SQL.

 

Dim rs as recordset

set rs=db.openrecordset({tablename or SQL})


rs.execute "INSERT INTO tb(ID,Name) VALUES (10,Anne)"



dim ar as ADODB.recordset

set ar=new adodb.recordset


ar.open {SQL Statement}
ar.execute "INSERT INTO tb(ID,Name) VALUES (10,Anne)"

These examples add a new record to the database directly.

  

Dim rs as recordset

set rs=db.openrecordset({tablename or SQL})


rs.addnew
rs!ID=intID
rs!Name=strName
rs.update

 c

dim ar as ADODB.recordset

set ar=new adodb.recordset


ar.open {SQL Statement}
ar.addnew
ar!ID=intID
ar!Name=strName
ar.update

These examples show how to edit a record directly, after the recordset is open it checks that there
is a record meeting the criteria in the open SQL. If not it creates one.

 
Dim rs as recordset

set rs=db.openrecordset("SELECT * FROM Tb WHERE tdID=10")


if rs.eof then
rs.addnew
else
rs.edit
end if
rs!ID=intID
rs!Name=strName
rs.update



dim ar as ADODB.recordset

set ar=new adodb.recordset


ar.open "SELECT * FROM Tb WHERE tdID=10"
if ar.eof then
ar.addnew
else
ar.edit
end if
ar!ID=intID
ar!Name=strName
ar.update

These examples show how to delete a record directly, after the recordset is open it checks that
there is a record meeting the criteria in the open SQL. If not it does not do a delete.

  

Dim rs as recordset

set rs=db.openrecordset("SELECT * FROM Tb WHERE tdID=10")


if not rs.eof then
rs.delete
end if

 

Dim ar as ADODB.recordset

set ar=new adodb.recordset


ar.open "SELECT * FROM Tb WHERE tdID=10"
if not ar.eof then
ar.delete
end if

Note: If you open an object when you have finished with it, close it and set it to nothing. For
example...

rs.close
set rs=nothing

This is good programming practice and clears the memory.

J ?
 ?$?? ??? %??? ???&'(??&?'?( ? ?
????????) ?? ???? ? ?? ? ??
?????  ? ???*?? ? ?$? ???? ??
+?! ?, ??$??&'(????* ?*? ????   ?

?? ??!%??? ?&'(??*???$  ?-?? ? ?? ?.??


?) -?!??!/??

  

îB has very poor documentation of its support for SQL. What SQL is or how it is used is a
mystery for most beginners simply because it is so darned hard to find out any information on
the topic! Those few references to SQL that exist in the îB documentation are very short
examples and there is virtually no discussion on how to create your own queries.

I assume that part of the reason for the Microsoft approach to SQL is that it sells a product called
SQL Server, which is a very powerful database interface. îB offers a limited set of the SQL
Server features but Microsoft doesn't highlight those capabilities, preferring instead to sell its
larger, more profitable product.

Considering the power of SQL statements I'm very surprised that Microsoft doesn't highlight the
features more than they do. However, the fact is that beginners have to look hard to find help so
that's where this tutorial comes into play.

J J  


Looking at the positive side of things, I think you'll find that SQL is so intuitive that just by
seeing a few examples you will gain a fair understanding of what is going on. Before I get into
some of the details about using SQL, here are some examples that should help you get the feel
for an SQL query.

"Select * From Title Where [Year Published] < 1889"


"Delete From Titles Where [Year Published] < #1/1/1889#"
"Select Name, Picture From Authors Where Date_of_Birth = #2/1/1947#"
"Select * From Employees"
"Select [First Name], [Last Name] From Employees"
"Select Employees, Department, SupvName From Supervisors, _
Employees Where Employees.Department = Supervisorts.Department"
"Select Distinct [Last Name] From Employees"
"Select [Last Name], Salary From Employees Where Salary > 2100"
"Select * From Orders Where [Shipped Date] = #5/12/93#"
"Select [Product Name], Sum ([Units in Stock]) From Products _
Group By [Product Name]"
"Select * From Employees Order By [Last Name], Asc"
"Select [Last Name], [First Name] From Employees Order by 2 Asc"
"Select [Last Name], Salary From Employees Order By Salary, _
Desc, [Last Name]

Three things to note about the examples:

O? 0 0??? ? ????


O? ?? ?!? ?%??0?0#1#12340??
O? º??*?*????? ??!0?5?6??

Now that you've read some of the examples, how do you use them? Simply set the RecordSource
property of a data control to an SQL statement such as those above and refresh the control like
this:

Data3.RecordSource = "SELECT * FROM Agency ORDER BY [City]"


Data3.Refresh

Just make sure that any references to fields match those contained in the actual database. Doing
so will create a recordset whose content will match the constraints described by the SQL
statement.

Considering that there are entire books on this subject, I can hardly expect to do it serious justice
but there are some basics which can be summarized in a short tutorial like this one.

First of all, there are 5 parts to an SQL statement which you should recognize:

’ ? ’?
  ?

?
  ??

? º *? ? 7 ? $??

 ? £?  ?
"? ??

? 8 ? ? 7
? &*??
?
&? 9$? ? :??
?
?
? ? (? :??
?
;? ??
? ? ?
?
? ? ? ?
With a little inspection you can pretty much guess what each of these pieces of an SQL statement
can do. However, here are a couple which you'll not want to miss and which I use pretty
regularly. Don't miss the last one in my list, which is a very easy way to sort a recordset!

O? J ?
???* ?!? **???) ??? ? ???  ??
O?  ?
&*%?!?$? ?*? ??*? ?? ?* ??
O? 
?
??!? *?????!? ??
O? £ 
?
??  ?!?? ??? *??!??
O? 

?
& ?? ?!?? *! ? ?? ?  ??

This should get you started. Try out a few of the examples on one of the databases which come
with îB and you'll see that it's really very easy to use the power of SQL!

l         

Japan Post has decided to incorporate fingerprint scanning in its ATM network rather than using
palm-scanning technology, sources said Saturday.

The postal service agency will introduce the Hitachi Ltd.


fingerprint technology to make its roughly 26,500
automated teller machines across the nation biometric by
the end of March 2009, the sources said.

Hitachi, the nation's largest comprehensive manufacturer


of electrical machinery, and computer maker Fujitsu
Ltd., which is pushing the palm-scanning technology,
have been locked in a fierce battle for the ATM market.

Japan Post is scheduled to issue integrated circuit cash ?

cards that can record holders' fingerprint data starting in  


   
 
   
October in a bid to prevent illegal withdrawal of   
  

      
deposits with the use of forged or stolen cards. 9 9?( ?9

#<,

?

It will remodel 12,000 of its ATMs to make them ?


biometric by the end of September and then replace the ?
remaining 14,500 in stages by the end of March 2009.

Part of the second-phase replacement will be undertaken by the postal savings bank, a unit that
will emerge from the planned privatization of the postal system starting in October 2007.
In addition to the postal savings bank, the privatization process will create three other entities
under a holding company -- a mail service company, a postal life insurance firm and a company
to manage the nationwide network of post offices.

You might also like