473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Issue with text box search. If match found, display form. If not, add new.

Hello,

I am having difficulty with Access VBA code. I am using Access 2010 (from work)/2013 (from home). I am creating a database that analyzes the nutritional quality of prepackaged foods for my place of employment, a local food bank.

Users enter a products UPC Code (which I use as the Primary Key). What I would like it to do, is if the product has been previously entered, the Product Form will open. If it is not yet in the database, I would like it to go to a new form with the UPC Code, which was previously entered to search, already populated.

I am able to get these to work individually, but as soon as I try to place them together, it fails. I have included the code that has worked below.

The following code opens a matching record to the appropriate form.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2.     DoCmd.OpenForm "frmProduct", acNormal, "", "[UPCCode]=[Forms]![frmUPCCodeSearch]![txtUPCCodeSearch]", acReadOnly, acNormal
  3.     DoCmd.Close acForm, "frmUPCCodeSearch"
  4. End Sub
  5.  
Then this code opens a new form with the UPC Code populated.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearchOpenForm_Click()
  2.     DoCmd.OpenForm "frmProduct", acNormal, acFormAdd, acWindowNormal
  3.         Forms!frmProduct!UPCCode = Me.txtUPCSearch
  4.     DoCmd.Close acForm, "frmUPCSearch"
  5. End Sub
  6.  
This is my first post here, so I apologize if I am missing anything, information or posting process. Please feel free to ask any questions or leave any comments. Any help on this would be amazing.

Thank you.

L
Sep 23 '14 #1
10 1527
jimatqsi
1,271 Expert 1GB
BouwmeesterL,
Welcome to Bytes.com.

You are not telling anything about the nature of the failure. What is that happens that should not happen, or what fails to happen that should?

You are closing two different forms, as seen in lines 3 of the first code block and line 4 of the second. Is that what you meant to do? Why close the calling form at all?

Jim
Sep 23 '14 #2
twinnyfo
3,653 Expert Mod 2GB
I am a bit confused, as Jim is, but I would approach a solution by first searching for the UPC code on your First Form. Then, if the UPC is found, open the Product Form, filtered by the UPC, if it is not found, then open the Product Form, but go to a new record. All this could be done from the same cmdOK_Click procedure.
Sep 23 '14 #3
I apologize for the lack of information and would like to thank you both for the quick response.

So twinnyfo, that is precisely what I am having difficulty doing. I would like to have both codes work from the same cmdOK_Click(), but have not been able to get them to work correctly which is why I separated the two for this purpose.

jimatqi, for my database. From the Home menu, you have the option to search for a UPC Code. Once that is selected the Search form (frmUPCCodeSearch) opens. Once the UPC Code has been entered and searched for, yes, I would like that form (frmUPCCodeSearch) to close to remove unneeded forms from the window.

When I combine the two codes, it has

A. Correctly open the Product form (frmProduct) with the matching record open. (which is precisely what I would like it to do).

B. When entering a product that is currently not entered in the database the Product form opens with only the header and footer visible. All other text boxes (where an individual enters in the nutritional quality information) is missing.

I have been playing around with it this morning and believe I may am on the way to a solution with a DCount syntax

Here it is...

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2. Private Sub cmdOK_Click()
  3.   If DCount("UPCCode", "tblProduct", "UPCCode = '" & Me.txtUPCCodeSearch & "'") > 0 Then 'Add new product
  4.         DoCmd.OpenForm "frmProduct", acNormal, acFormAdd, acWindowNormal
  5.             Forms!frmProduct!UPCCode = Me.txtUPCCodeSearch
  6.         DoCmd.Close acForm, "frmUPCCodeSearch"
  7.  
  8.     Else
  9.         DoCmd.OpenForm "frmProduct", acNormal, "", "[UPCCode]=[Forms]![frmUPCCodeSearch]![txtUPCCodeSearch]", acReadOnly, acNormal
  10.         DoCmd.Close acForm, "frmUPCCodeSearch"
  11.     End If
  12. End Sub
  13.  
When I try this. If there is a matching record, it goes to the Product form, all the text boxes are visible, but empty, with the exception of the UPC Code which is filled. (This is what I would like it to do if there is no matching record.)

If there is no matching record, the same thing that I previously mentioned with the no matching record occurs. Only the header and footer are visible.

I hope this information is more descriptive. Once again, any other questions, please feel free to ask. This has been driving me crazy.

Thank you.
Sep 23 '14 #4
twinnyfo
3,653 Expert Mod 2GB
B. When entering a product that is currently not entered in the database the Product form opens with only the header and footer visible. All other text boxes (where an individual enters in the nutritional quality information) is missing.
Check to make sure that your form's AllowAdditions Property is set to Yes
Sep 23 '14 #5
jforbes
1,107 Expert 1GB
I think your OpenForm call is missing some arguments. They are blank, but still need to be there for the arguments to be in the correct order. I think line 4 should be:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmProduct", acNormal, , , acFormAdd,acWindowNormal
Sep 23 '14 #6
zmbd
5,501 Expert Mod 4TB
Jforbs is correct, when using the shortended form, the order of the entries is vital.

This is why I prefer using named augments for all but the simplest of methods (think Msgbox) and suggest that most new programmers use the same method.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm _
  2.         FormName:="frmProduct", _
  3.         View:=acNormal, _
  4.         DataMode:=acFormAdd, _
  5.         WindowMode:=acWindowNormal
  6.  
You can scramble the order of "FormName:=", "View:=", "DataMode:=", and "WindowMode:=" and it wouldn't matter... so long as the ":=" is used and the correct information is supplied.
Sep 23 '14 #7
Thank you for your help, but I could not get that to work. It displayed a compile error over View: "Expected: End of Statement".

After speaking with some other individuals, I was informed that something so simple would not accomplish what I am trying to automate. This was sent to me.

Expand|Select|Wrap|Line Numbers
  1. The syntax is completely incorrect, but this is the gist of what you need.
  2.  
  3. i = count of rows in the table
  4. searchResult
  5. enteredUPC = the upc the user typed in
  6. UPCColumn = the column in the table that stores the UPC codes already entered
  7.  
  8. For 1 to i (where i is a count of the rows in the tables)
  9.   If enteredUPC = UPCColumn.i then (UPCColumn.i is the Column in the table that stores the UPC Code and i represent the current row to compare to the user-entered UPC)
  10.      searchResult = True
  11.      exit
  12.   Else
  13.   End if
  14. Next i
  15.  
  16. If searchResult = True
  17.   LoadForm-Results
  18. Else
  19.   LoadForm-EnterInformation
  20. End if
  21.  
  22.  
From that, the following is what I have come up with:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2.  
  3.     Dim dbs As Database
  4.     Dim rst As Recordset
  5.     Dim a As Integer
  6.     Dim i As Integer
  7.     Dim Check As Long
  8.     Dim bResult As Boolean
  9.     Dim sUPCSearch As String
  10.     Dim sCodeKey As String
  11.  
  12.     'Get the database and recordset
  13.     Set dbs = CurrentDb
  14.     Set iCount = DCount("*", tblProduct)
  15.     sUPCSearch = [Forms]![frmUPCCodeSearch]![txtUPCCodeSearch]
  16.     sCodeKey = (tblProduct.UPCCode)
  17.  
  18.     For Check = 1 To iCount
  19.         If sUPCSearch = [tblProduct]![UPCCode].iCount Then
  20.         'UPCCode in tblProducts and UBound represent the current row to compare to the user-entered UPC)
  21.             bResult = True
  22.         Else
  23.         End If
  24.  
  25.     Next
  26.  
  27.     If bResult = True Then
  28.         DoCmd.OpenForm "frmProduct", acNormal, "", "[UPCCode]=[Forms]![frmUPCCodeSearch]![txtUPCCodeSearch]", acReadOnly, acNormal
  29.         DoCmd.Close acForm, "frmUPCCodeSearch"
  30.     Else
  31.         DoCmd.OpenForm "frmProduct", acNormal, acFormAdd, acWindowNormal
  32.             Forms!frmProduct!UPCCode = Forms!frmUPCCodeSearch!txtUPCCodeSearch
  33.         DoCmd.Close acForm, "frmUPCCodeSearch"
  34.     End If
  35.  
  36. End Sub
  37.  
It is still having issues with the DCount. I used that because I couldn't figure out any other ways to get a dynamic count on the records in the table.

I apologize, but I feel like I am going crazy here with the amount of different ways I am trying to make this work. And for all I know, I am now just completely over-complicating the process.

Thanks.
Sep 24 '14 #8
jforbes
1,107 Expert 1GB
I think your buddies may mean well, but I think they are leading you away from your solution. I also think what they are recommending is more of a .NET solution. Since you are in MS Access, you can shortcut some of the things they are doing.

I created a project with a table and a couple forms using your names an pasted the your code into it and there were a few things I had to fix to get it working, but you were really close:
  • Reversed the operator on the DCount evaluation.
  • Tweaked the Where clause to be a little more straight forward and to use Single Quotes for the UPC. If your UPC is a Number, you'll need to remove the Single Quotes.
  • Added the blank Arguments for the DoCmd.Open
  • Changed the references to the TextBox to use the Value of the TextBox

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2.     If DCount("UPCCode", "tblProduct", "UPCCode = '" & Me.txtUPCCodeSearch.Value & "'") = 0 Then
  3.         'Add New Product
  4.         DoCmd.OpenForm "frmProduct", acNormal, , , acFormAdd, acWindowNormal
  5.         Forms!frmProduct!UPCCode = Me.txtUPCCodeSearch.Value
  6.         DoCmd.Close acForm, "frmUPCCodeSearch"
  7.     Else
  8.         ' Display Existing Product
  9.         DoCmd.OpenForm "frmProduct", acNormal, , "[UPCCode]='" & Me.txtUPCCodeSearch.Value & "'", acReadOnly, acNormal
  10.         DoCmd.Close acForm, "frmUPCCodeSearch"
  11.     End If
  12. End Sub
  13.  
Let us know how things go.
Sep 24 '14 #9
Rabbit
12,516 Expert Mod 8TB
The key takeaway from jforbes' modified code is that you reversed your code in post #4. You were checking if the count was greater than 0, then add a new record. Otherwise, open the record. But that's backwards, if the count is greater than 0, then the record exists so you shouldn't be adding a new record.
Sep 24 '14 #10
Wonderful! Thank you so much for the time you have all taken. It works perfectly.


Thank you.
Sep 24 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Seb Delcampe | last post by:
Hello, I am facing two problems with Full-text search. 1° My query looks like: SELECT id, title FROM myTable WHERE ((MATCH(title) AGAINST ('-myword' IN BOOLEAN MODE))) I thought this...
3
by: Andrew Crowe | last post by:
Hi guys, I have a table currently set up like this: <- -> video ------ video_id
1
by: arikatla | last post by:
We are using SQL Server 2000 database (with sp3) and recently we faced an interesting issue with full text search. According to SQLServer help page "AND | AND NOT | OR Specifies a logical...
9
by: Christopher Koh | last post by:
I will make a form which will search the database (just like google interface) that will look/match for the exact name in the records of a given fieldname. Any suggestions on how to make the code?
2
by: Zeya | last post by:
I have an application where I am required to run a full text query. My database is MySQL and code in C#. The way all queries are SELECT * FROM table WHERE firstname = ? and using command...
0
by: coosa | last post by:
Dear all, I'm using MS SQL Server 2005 and i have a full-text search issue; Let's say i have table1, table2 and table 3; for each, some full-text indices have been created. The issue is that i...
3
by: Alexandre | last post by:
Hi! I receive this error in my webapp: Ambiguous match found. At line: Line 1: <%@ page language="C#" masterpagefile="~/memberscontents/master_interna.master" autoeventwireup="true"...
1
by: sakhan | last post by:
Hello Friends, Could some body help me with the ASP Text Search. What I am looking broad text search, when I type in a string in the text box and press the go button beside it, it should give...
8
by: Sham | last post by:
I am trying to perform the following query on a table that has been indexed using Full Text Search. The table contains multiple columns than have been indexed. (Below, all xml columns are...
0
by: padmajapenmetsa | last post by:
Hi, We have the full text search table and are using CONTAINSTABLE to search in the fulltext search column, its workingfine, but we have a requirement that we need to return results which match a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.