UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Help With Access 2016 Search Button, Access 2016    
 
   
Adrian_Corn
post Jun 9 2020, 01:36 PM
Post#1



Posts: 17
Joined: 15-April 20




Good Evening all

I wonder if somebody could look at the following code that I found on the internet, i think i have changed all the field names for searching correctly, and yet i still cannot get it to run

I have placed an unbound text box on my form as instructed, and in theory by pressing the button on the form it should display the record in my form

'--------------------------------------------------------------
'GJT 25-01-02
'--------------------------------------------------------------
Private Sub cmdSearch_Click()
Dim strREG As String
Dim StrSearch As String

'Check txtSearch for Null value or Nill Entry first.

If IsNull(Me![txtsearch]) Or (Me![txtsearch]) = "" Then
MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
Me![txtsearch].SetFocus
Exit Sub
End If
'---------------------------------------------------------------

'Performs the search using value entered into txtSearch
'and evaluates this against values in strN-Number

DoCmd.ShowAllRecords
DoCmd.GoToControl ("strREG")
DoCmd.FindRecord Me!txtsearch

strstrREG.SetFocus
strstrREG = strstrREG.Text
txtsearch.SetFocus <------------- COMPILE ERROR: METHOD OR DATA MEMBER NOT FOUND
StrSearch = txtsearch.Text

'If matching record found sets focus in strREGand shows msgbox
'and clears search control

If strREG = StrSearch Then
MsgBox "Match Found For: " & StrSearch, , "Congratulations!"
strstrREG.SetFocus
txtsearch = ""

'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "Match Not Found For: " & StrSearch & " - Please Try Again.", _
, "Invalid Search Criterion!"
txtsearch.SetFocus
End If
End Sub



The field i am trying to search is called REG (in a database called US Civil Aircraft Register)
The button for the search is called cmdsearch
The label for the unbound text box is called txtsearch
The unbound text box is named search ( label name = txtsearch )

Sorry i'm a bit new at this coding....

Regards

Adrian

Go to the top of the page
 
theDBguy
post Jun 9 2020, 01:40 PM
Post#2


UA Moderator
Posts: 78,476
Joined: 19-June 07
From: SunnySandyEggo


Hi Adrian.

You can't set the focus to a label. Try changing this:

txtsearch.SetFocus <------------- COMPILE ERROR: METHOD OR DATA MEMBER NOT FOUND
StrSearch = txtsearch.Text

into this:

Me.search.SetFocus
StrSearch = Me.search

Hope that helps...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
June7
post Jun 9 2020, 01:56 PM
Post#3



Posts: 1,527
Joined: 25-January 16
From: The Great Land


@theDBguy, if this is label, then could not enter data into it. Other code would fail as well.

@Adrian, please post lengthy code between CODE tags to retain indentation and readability.
Was your source for VB.net? VBA use Value property to get value of control, not Text. Text property can only be used when control has focus. Review this tutorial http://allenbrowne.com/ser-62.html

--------------------
Attachments Manager is below the edit post window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
Go to the top of the page
 
theDBguy
post Jun 9 2020, 02:15 PM
Post#4


UA Moderator
Posts: 78,476
Joined: 19-June 07
From: SunnySandyEggo


QUOTE (June7)
@theDBguy, if this is label, then could not enter data into it. Other code would fail as well.

@June7, I guess you have a point there. I was just going by this statement from the OP.

QUOTE (Adrian_Corn)
The label for the unbound text box is called txtsearch
The unbound text box is named search ( label name = txtsearch )

I guess we'll have to wait for some clarification. Cheers!

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
Adrian_Corn
post Jun 9 2020, 04:10 PM
Post#5



Posts: 17
Joined: 15-April 20



Many Thanks Guys,

I attach I copy of the database and form, as i am still a little confused regarding the code....

Regards

Adrian
This post has been edited by Adrian_Corn: Jun 9 2020, 04:15 PM
Attached File(s)
Attached File  Utter_access__2_.zip ( 1.87MB )Number of downloads: 3
 
Go to the top of the page
 
GroverParkGeorge
post Jun 9 2020, 06:24 PM
Post#6


UA Admin
Posts: 37,482
Joined: 20-June 02
From: Newcastle, WA


I modified the "search" function in your accdb. It now works IF your user types in a valid REG value.

I changed the form's record source to use tempvars for filtering.

I changed the form's recordsource to return ONE and ONLY one record, at most. This is in keeping with good practice that discourages loading entire tables into an open form.

I changed table and field names to eliminate spaces and non-standard characters. This is in keeping with good practice that discourages spaces in object and property names.

I changed the way the two tables relate to use the Primary Key instead of one of the property keys.

Attached File  Utter_access.zip ( 37.72K )Number of downloads: 5

--------------------
My Real Name Is George. Grover Park Consulting is where I did business for 20 years.
How to Ask a Good Question
Beginning SQL Server
Go to the top of the page
 
Adrian_Corn
post Jun 10 2020, 12:19 AM
Post#7



Posts: 17
Joined: 15-April 20




Morning George,

That is Brilliant, and works perfectly

Many Thanks for your time.

Best Regards

Adrian
Go to the top of the page
 
GroverParkGeorge
post Jun 10 2020, 07:40 AM
Post#8


UA Admin
Posts: 37,482
Joined: 20-June 02
From: Newcastle, WA


We are happy to help.

A bit of editorializing. Over time I've come to the conclusion that some things are always true. One of those is that simpler is almost always better. I prefer solutions that rely on the least possible amount of code and the fewest changes to objects. That was the path leading to the IIF() and Tempvars approach to filtering/searching tasks.

--------------------
My Real Name Is George. Grover Park Consulting is where I did business for 20 years.
How to Ask a Good Question
Beginning SQL Server
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    9th July 2020 - 12:02 AM