Hi,
I am trying to do something that seems fairly easy, but I’m having some difficultly and hopefully someone can help. Here is a high level over of what I’m trying to do:
I have two tables, Table 1 named OLD and Table 2 named NEW. Table 1 is a small group of records and Table 2 has a large quantity of records. They are both very similar in their content where they both contain fields for a PART NUMBER, MANUFACTURER’S NAME, PIECE PRICE, etc..
My goal is to find all records in Table 2 whose values in the PART NUMBER field (PN) match or “closely match” the PART NUMBER (PN) value in Table 1. In most cases where the two records are actually a match, the PN data from the two tables won’t match exactly. So to find records in Table 2 that “closely match” Table I did the following using standard queries.
* I created 2 forms;
- Frm1 uses all the data from Table 1 (OLD)
- Frm2 uses all the data from Table 2 (NEW)
* First I open Frm1.
* The query behind Frm1 has the fields from Table 1, but it also has an expression named MidPN that is the mid$ of a field in the table, PN. In my example I’m using Mid$([PN],3,10). I can’t have 3 and 10 hard coded, more on that later.
* I have placed MidPN on Frm1
* While in Frm1 Form I click on a button to open up Frm2
* In Frm2's query I have a condition that looks at the mid$ expression from Frm1, MidPN, and only opens records in Frm2 that match
* The unique aspect of this is that I add wild cards to either side of the mid$ expression so it will find all "like" matches (Like "*"+[forms]![Frm1]![MidPN]+"*")
Using standard queries this works perfectly. I can scroll through Frm1, hit the "open Frm2" button and the list of matching records comes up. It works Great.
WHAT I WANT TO DO THUS WHY I NEED TO OPEN THE FORM IN VBA.
1) In Frm1 I'd like the mid$ variables to be user defined. Ideally they would set them before opening the Frm1 (say 3 characters in and length of 10). Then, as the user scrolls through Frm1 they could change these two values at any time before opening Frm2. This would allow them to either expand or narrow their search based on the mid$ values. When they went to the next record in Frm1 the values would revert to the initial setting (in this case 3 and 10). But again, if they needed to they could change before once again opening Frm2.
2) When they open Frm2 I'd like to keep Frm1 open and visible. They need both forms open because they must determine if the two records are actually a match. This is best done if they can view both sets of data. While both forms are open, if they determine there is a match, they need to be able to click on a check box in Frm2. Is it possible to tile forms horizontally and go back and forth between the two forms?
All this could be solved if there was a one to many link that could use the “like” expression, but I don’t think this is possible.
THIS IS SQL STATEMENT DIRECTLY FROM QUERY BEHIND FRM1:
SELECT Mid$([PN],1,8) AS MidPN, OLD.PN, OLD.Manufacturer, OLD.Description, OLD.Value, OLD.Designator, OLD.Quantity, OLD.Piece_Price, OLD.Vendor, OLD.Notes
FROM OLD;
THIS IS SQL STATEMENT DIRECTLY FROM QUERY BEHIND FRM2:
SELECT NEW.match, NEW.PN, NEW.Manufacturer, NEW.Description, NEW.Value, NEW.Designator, NEW.Quantity, NEW.Piece_Price, NEW.Vendor, NEW.Notes
FROM NEW
WHERE (((NEW.Value) Like "*"+[forms]![Frm1]![MidPN]+"*"));
BELOW IS MY VBA CODE WHERE I TRY TO DUPLICATE WHAT ACCURS IN THE QUERIES:
The key is the “WHERE” statement. In my example below it doesn’t try and use the “wild cards” with the mid$ statement because I couldn’t get it working. That is where I need help because I’ve tried a ton of different syntaxes, but continue to get errors.
Dim strSQL As String
'
strSQL = "SELECT New.[PN], New.[Manufacturer], New.[Value], New.[Description], New.[Designator], New.[Quantity], New.[Vendor], New.[Notes]" & _
"FROM New " & _
"WHERE New.[PN] ='" & [Forms]![Frm1]![MidPN] & "' ; "
'
Set rs = CurrentDb.OpenRecordset(strSQL)
DoCmd.OpenForm "frm2", , , , , acNormal
Forms!frm2.RecordSource = strSQL
Forms!frm2.Visible = True
'
End Sub
So, to recap my questions:
* How can I code the WHERE statement so it works like the wild card configuration in the standard query?
* How can I have the mid$ values set up as a variable so the user can change?
* Can I have both Frm1 and Frm2 open at the same time and can the user edit them?
Thanks in advance for your help. If I can get this working it will help me tremendously.