Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2005, 04:45 PM
Authorized User
 
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Default Running a search on database from form

Hi all,

I am trying to create a search form for my database. Basically I just need to search for the records by one field. Now after that person enters in the criteria for the search in the text box there is a command button on the bottom of the form that will open the other form with the record on it. I want the person to be able to click the command button and open the other form with the ability to edit the record that matches the criteria that they entered. Also I want a message box that pops up if the criteria does not match what that person entered. I am sure I have to do this with a SQL query but not sure how to. Any help would be greatly appreciated.

Chris Kuznicki
 
Old February 3rd, 2005, 01:04 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Select Field1,Field2 from TableName Where SearchField like '%SearchText%'

This query would search the field for the text entered by the user. You can populate this result in the window and code for edit functionality there.

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old February 3rd, 2005, 08:54 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

What you want to do is to create an unbound form with a combo box and a button.

Use the combo box wizard to select the field from the table you would like to populate the box with.

Then place a button on the form using the button wizard, and have that wizard open the form in edit mode (select Open Form from the list).

You will get button code that looks like this on the button's On Click Event:
'==========
Private Sub btnOpenForm1_Click()
On Error GoTo Err_btnOpenForm1_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Form1"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_btnOpenForm1_Click:
    Exit Sub

Err_btnOpenForm1_Click:
    MsgBox Err.Description
    Resume Exit_btnOpenForm1_Click

End Sub
'==========

Notice the stLinkCriteria variable. Notice also that it is not defined. To define it, add this code:

'==========
Private Sub btnOpenForm1_Click()
On Error GoTo Err_btnOpenForm1_Click

    Dim stDocName As String
    Dim stLinkingData As String
    Dim stLinkCriteria As String

    stLinkingData = Me.cboYourComboBox
    stLinkCriteria = "[PKField] = " & "'" & stLinkingData & "'"

    stDocName = "Form1"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_btnOpenForm1_Click:
    Exit Sub

Err_btnOpenForm1_Click:
    MsgBox Err.Description
    Resume Exit_btnOpenForm1_Click

End Sub
'==========

That should do it.

Please note that the combo box, while showing meaningful data, is actually bound to the Primary Key field [PKField] in your look up table, so that is the data you are collecting to pass to the form.

If you limit the combo box to the list, then you won't have to worry about error handling for invalid data. The error handlers will do that automatically.

One of the advantages of this method is that it only calls the data that the form needs for that record, and not all the records, so it should work pretty quickly.

To accomodate your users, you may want to put another combo box on the form that is opened so that once it is opened, the user can look up another record without having to go back to the main form again.

Use the qizard to add a combo box that looks up the same data (NOT to find a record on your form!) and then put this code in the combo box's After Update Event:

'==========
Me.RecordSource = "SELECT * from tblYourTable WHERE PKField = " & Str(cboNewComboBox)
'==========

When a user selects meaningful data from the combo box, the form will reload with the new data. Note the Primary Key is still being passed.

HTH


mmcdonal
 
Old February 3rd, 2005, 06:37 PM
Authorized User
 
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks all,

The code that mmcdonal gave me to use is exactly what I needed and had the added benefit of looking more professional than I expected. Now I have another question. I want to take data that is in one field and make a chart of it. Basically I want it to be a pie chart showing comparison between two values. The field stores a number one or two depending on the option that the user selects in a form. Do I somehow make a query that would separate these values. What should my approach be?

Thanks,

Chris





Similar Threads
Thread Thread Starter Forum Replies Last Post
Only one form running in runtime. qiux General .NET 6 January 14th, 2008 09:44 PM
Calculate running total on form and prevent text Hughesie78 ASP.NET 2.0 Basics 0 November 27th, 2007 10:35 AM
running example code from Ch 3 - Form Validation cshu BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 November 4th, 2006 05:52 AM
running macro/user form from other excel files kliu9 Excel VBA 2 April 7th, 2005 11:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.