472,378 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

Searching multiple tables in Access using Visual Basic

I am trying to write a piece of code that will search through a number
of different tables (current one being tableNm) to look for a specific
street name that has been entered by the user and saved as
Enteredstreet. If the street is found I then want to display a message
box saying what table the street name is in. The code I am currently
trying to use is this:

Sub dbOpentableX()

Dim dbsAddSampling As Database
Dim rstTable As Recordset

Set dbsAddSampling = CurrentDb
Set rstTable = dbsAddSampling.OpenRecordset(tableNm)

With rstTable
.Index = "STREETNM"

Do While Not .EOF
If !STREETNM = Enteredstreet Then
MsgBox "Street name found in" & tableNm

Exit Do

Else
.MoveNext

End If

Loop

End With

End Sub

But when I try to run this an error message comes up saying
'User-defined type not defined' and then highlights the bit of code
saying 'dbsAddSampling As Database'. I have probably done something
really stupid, or else the code is just rubbish, but I would
appreciate any help.

Katrina
Nov 13 '05 #1
3 5883
Katrina wrote:
I am trying to write a piece of code that will search through a number
of different tables (current one being tableNm) to look for a specific
street name that has been entered by the user and saved as
Enteredstreet. If the street is found I then want to display a message
box saying what table the street name is in. The code I am currently
trying to use is this:

Sub dbOpentableX()

Dim dbsAddSampling As Database
Dim rstTable As Recordset

Set dbsAddSampling = CurrentDb
Set rstTable = dbsAddSampling.OpenRecordset(tableNm)

With rstTable
.Index = "STREETNM"

Do While Not .EOF
If !STREETNM = Enteredstreet Then
MsgBox "Street name found in" & tableNm

Exit Do

Else
.MoveNext

End If

Loop

End With

End Sub

But when I try to run this an error message comes up saying
'User-defined type not defined' and then highlights the bit of code
saying 'dbsAddSampling As Database'. I have probably done something
really stupid, or else the code is just rubbish, but I would
appreciate any help.

Katrina


You might try

If (CurrentProject.Connection.Execute( _
"SELECT * FROM tbl2002Transactions " _
& "WHERE fldDescription = """ & Description & """").BOF) Then
MsgBox Description & " not found"
Else
MsgBox Description & " found"
End If

substituting your own variables.

--
--
Lyle
--
From ADO28.chm

Deprecated Components
Each of the following components is considered obsolete. While these
components are still supported in this release of the Microsoft® Data
Access Components (MDAC), they may be removed in the future. When
writing new applications, you should avoid using these deprecated
components. When modifying existing applications, you are strongly
encouraged to remove any dependency on these components.

ODBC Provider (MSDASQL)
You are strongly encouraged to use one of the native OLE DB Providers
instead of the Microsoft Open Database Connectivity (ODBC) Provider.
Native OLE DB Providers provide better application stability and
performance. Furthermore, native OLE DB Providers will be supported in
the future, whereas MSDASQL will not have any new features added to it,
will not be available on 64-bit, and will not be accessible from the OLE
DB NET Data Provider.

Remote Data Services (RDS)
Remote Data Services (RDS) is a proprietary Microsoft mechanism for
accessing remote data across the Internet or intranet. Microsoft is now
shipping the Microsoft Simple Object Access Protocol (SOAP) Toolkit 2.0
that enables you to access remote data using an open, XML-based
standard. Given the availability of the SOAP Toolkit 2.0, you should
migrate from RDS to SOAP. The SOAP 2.0 Toolkit 2.0 also includes sample
code for remotely accessing Microsoft ActiveX® Data Objects (ADO)
Recordsets.

Jet and Replication Objects (JRO)
The Microsoft Jet OLE DB Provider and other related components were
removed from MDAC 2.6. Microsoft has deprecated the Microsoft Jet
Engine, and plans no new releases or service packs for this component.
As a result, the Jet and Replication Objects (JRO) is being deprecated
in this release and will not be available in any future MDAC releases.

.....
Nov 13 '05 #2

"Katrina" <tr******@hotmail.com> wrote in message
news:8c**************************@posting.google.c om...
I am trying to write a piece of code that will search through a number
of different tables (current one being tableNm) to look for a specific
street name that has been entered by the user and saved as
Enteredstreet. If the street is found I then want to display a message
box saying what table the street name is in. The code I am currently
trying to use is this:

Sub dbOpentableX()

Dim dbsAddSampling As Database
Dim rstTable As Recordset

Set dbsAddSampling = CurrentDb
Set rstTable = dbsAddSampling.OpenRecordset(tableNm)

With rstTable
.Index = "STREETNM"

Do While Not .EOF
If !STREETNM = Enteredstreet Then
MsgBox "Street name found in" & tableNm

Exit Do

Else
.MoveNext

End If

Loop

End With

End Sub

But when I try to run this an error message comes up saying
'User-defined type not defined' and then highlights the bit of code
saying 'dbsAddSampling As Database'. I have probably done something
really stupid, or else the code is just rubbish, but I would
appreciate any help.

Katrina

The first problem is references. While viewing your code, select
Tools>References and make sure you have a reference set to Microsoft DAO 3.6
Object Library. Remove any references you don't need, such as ActiveX Data
Objects. You could alter the code to:
Dim dbsAddSampling As DAO.Database
Dim rstTable As DAO.Recordset
To make sure it is clear that you are using the DAO object library.

The next comment is that something must be a bit funny about the database
structure if you have to search multiple tables for the same thing.
Normally, you would expect similar information to be stored in a single
table. Perhaps that is out of your control.

The third is that it probably doesn't make sense to open an editable
recordset based on the whole table. It is a bit wasteful since you only
need to find if there is a matching entry or not. This recordset would be
better:
strSQL="SELECT STREETNM FROM MY_TABLE WHERE STREETNM=""" & strStreet & """"
Set rst=dbs.OpenRecordset(strSQL, dbOpenForwardOnly)
However, you could use other alternatives or even scrap most of the code and
use a function like DLOOKUP, DFIRST or similar.
Nov 13 '05 #3
Thanks Justin. What you suggested worked!!!

Katrina

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BlackFireNova | last post by:
Using Access 2002 I am writing a report which draws data from several different tables. I can't link all the tables in a query, as some can not be related without truncating the data. I plan...
4
by: anand | last post by:
Hi, I have an Access 2000 database, which contains some native tables, and some linked tables which belong to an ORACLE database, through ODBC. Using VB.NET, I am trying to fetch some data by...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
9
by: cj | last post by:
I'm trying to forge ahead with Visual Basic .Net but recently I've suffered several major set backs in demonstrating VB is the future and we should move from Visual FoxPro. I really need to find...
0
by: RLN | last post by:
I have a Microsoft Access2002 database that needs to connect to an Oracle Database. I need to map 2 tables from the Oracle DB to retrieve the proper data. I read somewhere (quite a while back)...
21
by: Al Christoph | last post by:
I posted this last week end in the MSDN forums. No luck there. Let's see what the experts here have to say:-)))) I have a rather convoluted project. The distributable will come in eight...
7
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent...
6
by: Dave | last post by:
On my form I have combo boxes. These combo boxes, after updating them, populate respective listboxes that are located below the combo boxes on the same form. I am trying to use a "generate...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.