Microsoft Visual Basic 6.0 for PC Logo

Related Topics:

Posted on Mar 14, 2009

Microsoft Access query in VB

How can i run a Microsoft Access query in VB?
Example: I want to run a select query that shows all records with the data i input in a textbox.

1 Answer

Anonymous

Level 1:

An expert who has achieved level 1.

  • Contributor 1 Answer
  • Posted on Mar 17, 2009
Anonymous
Contributor
Level 1:

An expert who has achieved level 1.

Joined: Mar 17, 2009
Answers
1
Questions
0
Helped
243
Points
2

First you need to create a record set to do this, after creating a record set RS1. please follow the code below:
Update the code as per your requirement.
Rs1.Open "Select * from table_name where UserName = '" & UName & "'", Cn1, adOpenDynamic, adLockOptimistic

Add Your Answer

×

Uploading: 0%

my-video-file.mp4

Complete. Click "Add" to insert your video. Add

×

Loading...
Loading...

Related Questions:

0helpful
1answer

How to copy a rangewithout scrolling in Microsoft Access step-by-step. Then how to paste the copied information to Excel 2010.

you said time was an issue !

Have a look at this software tool for access database comparison
http://www.fmsinc.com/products/detective/index.html

It costs money but if time is an issue it might be worth buying to get you out of a jam !
0helpful
1answer

How to invoke a ms.access using parser

To parse a Text field that contains two words separated by a comma, follow these steps:
  1. Open any existing database.
  2. Create a table with the following structure: Table: Parse2Words
    ------------------
    Field Name: Empl
    Data Type: Text
  3. View the Parse2Words table in Datasheet view and type the following three records in the Empl field: Smith, John
    Callahan, Laura
    Fuller, Andrew
  4. Create the following query based on the Parse2Words table: Query: QueryTest
    ------------------------------------------------------------------
    Field: FirstName: Right$([Empl],Len([Empl])- InStr(1,[Empl],",")-1)
    Show: True
    Field: LastName: Left$([Empl],InStr(1,[Empl],",")-1)
    Show: True

    NOTE: You can modify the QueryTest query to account for spaces between the two parts in the Empl field. For example, if the text in the Empl field is "Smith,John" without spaces, remove the -1 from the FirstName field expression.
  5. Run the query. Note that the QueryTest query separates the text in the Empl field into the two fields below: FirstName LastName
    --------------------
    John Smith
    Laura Callahan
    Andrew Fuller
0helpful
1answer

Using Microsoft Query to bring Access 2007 data into a spreadsheet

Try using Data > From Other Sources and select From Microsoft Query. In the Excel sheet, select the cell that you want the results to start in. Your MS Query would be pulling the data from your Access database.
1helpful
1answer

I want searching code for visual basic 2008....which can search the required information from a database...in my case database is FIR...if i input the FIR number..i want to show the correspoding...

Not much code needed in Visual Basic for this one. Visual Basic would only show the data actually. The search on the database is actually just one query statement. query = Select * from databasename.tablename where FIRnumber=?. where ? is the input in a textbox.
0helpful
1answer

What is the compatible database for visual basic 6.0?

Microsoft Access uses the Jet Engine. In VB 6, you can create a data connection using ODBC or OLE DB. Here is an example of OLE DB:

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\YourAccessDB.mdb;User Id=;Password=;" Good luck
4helpful
1answer

How to connect vb with ms access

se Adodc OR Even better.. ADODB. for this you need to get MDAC fromMicrosoft free download.(check under project_References- Microsoft DataActiveX objects.. the current no is 2.8. if it is not there download invb directory.

next, you can learn about creating a DSN from control panel, ODBC. onceyou learn this, connection becomes a piece of cake. get some free vbtutoriasl from WWW.

then

you need to use DAO or ADO to connect to the database. better study both but since ADO is current, i am showing that code.
under Menu Project_References, put a check mark on Microsoft ActiveXData Objects latest version (though it works for all, currently 2.8with sp pack 1 ).

at the general declarations:
dim conn as adodb.connection, rec as adodb.recordset, esql as string,esql1 as string
Private Function connect()
Set rec = New ADODB.Recordset
Set conn = New ADODB.Connection
esql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "D:VBDesigndhana2.mdb" & ";Persist Security Info=False"
'here you put the correct path of your mdb file. and check if you have jet 4.0

conn.Open (esql), , , 0
End Function

private sub form_Load()
connect
end sub
and for adding records:
say let us say you have 3 fields. then have 3 textboxes and 1 command button on the form.

private command1_click()
esql1="select * from Yourtablename"
rec.open(esql1),conn,, adOpenDynamic, adLockOptimistic
rec.AddNew
rec.Fields(0) = text1.Text
rec.Fields(1) = Text2.text
rec.Fields(2) = text3.text
' you need to check if in your access table design you have Allowed Zero length .. set it to Yes for all text fields.
rec.update
if not rec.eof then rec.movenext
rec.close
conn.close
set conn to nothing
end sub

this will add new records to access table from VB.
' for picture store the full path of the path and the picture file nameint the text field. and in the picturebox of VB form for viewingrecords again you need ADO or ADODC and here you code:picture1.picture=Loadpicture(rec.fields(3) ' depending on where thepicture field is located. you need to make a few trials.
under Menu Project_References, put a check mark on Microsoft ActiveXData Objects latest version (though it works for all, currently 2.8with sp pack 1 ).

at the general declarations:
dim conn as adodb.connection, rec as adodb.recordset, esql as string,esql1 as string

Private Function connect()
Set rec = New ADODB.Recordset
Set conn = New ADODB.Connection
esql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "D:VBDesigndhana2.mdb" & ";Persist Security Info=False"
'here you put the correct path of your mdb file. and check if you have jet 4.0

conn.Open (esql), , , 0
End Function

private sub form_Load()
connect
end sub

and for accessing records:

private command1_click()
esql1="select * from Yourtablename where id =" & val(text1) ' for integers

esql1="select * from Yourtablename where name=" & "'" & text1 & "'" ' for string

rec.open(esql1),conn, adOpenstatic, adLockreadonly

label1.caption =rec.Fields(0)
label2.caption= rec.Fields(1)
label3.caption= rec.Fields(2)
' you need to check if in your access table design you have Allowed Zero length .. set it to Yes for all text fields.

rec.close
conn.close
set conn to nothing
end sub


' similarly there are routines to ADD, Delete, Edit, View etc.
0helpful
1answer

Program not working

Type mismatch is caused by trying to use a variable defeined as one type to store another type of data. For example a variable that is integer but you try to set it to a letter.
In VB.net this can be caused by a line like this:
textbox="hello"
when it should be
textbox.text="hello"
see if you have that sort of mistake.
0helpful
1answer

Ms access

Stored Procedures don't exist in MS Access. You do have the following options though. Queries, which are just select or action queries. VB Code, which you can write using Query Objects as if you were writing a Visual Basic application. These can be Functions or Subroutines. In these, you would use the standard programming techniques with while and for loops, etc.

You can create complex situations combining the two of these. A function in the VB code area can accept through parameters, the single values (line by line, record by record) in a query and act on them and manipulate the values.

For example: create a query that does a select phone from address. Create a function in vb called public function StripDashesInPhone(Phone as string) which then uses VB coding to strip dashes from each phone value passed in. To make it all work, in the query on one of the field columns put "NewPhone: StripDashesInPhone([Phone])" and for every record processed in the query, the function is called with the [Phone] field value passed in to the function and the action is processed and returned.

Other than writing a function that is activated by a form button click, which opens the current db and opens a table and process it, just like in VB, this is about as close to cursors and oracle procedures as you get.

I wish it was more, but ...
4helpful
4answers

Running SQL queries on Excel

Another way to do it is to use the SQL Drill freeware Excel addin (http://www.sqldrill.com)
hth
Not finding what you are looking for?

253 views

Ask a Question

Usually answered in minutes!

Top Microsoft Computers & Internet Experts

Grand Canyon Tech
Grand Canyon Tech

Level 3 Expert

3867 Answers

k24674

Level 3 Expert

8093 Answers

Brad Brown

Level 3 Expert

19187 Answers

Are you a Microsoft Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides

Loading...