How to check if value exist in two tables - ms-access

Hi I've searched the net and couldn't find the right answer to my question.
I have two tables GCSALLDATA and GCS-RECONCILED. They both have the same columns but I only need to focus on two [Control Number] (Short Text) and [NotInDevTrack] (yes/no)
I want to search GCSALLDATA for the Control Number found in GS-RECONCILED. If it's found Update the record.
From what I read using a DCOUNT should be able to do this, but wondered how would it cycle through all the records in the table?
Here is my attempt at using to use it
If DCount("[Control Number]", "GCSALLDATA", "Control Number=" & [GCS_Reconcile].[Control Number]) > 0 Then
MsgBox ("Control number already in use")
Else
MsgBox ("Control Number missing add it")
End If

Assuming I've correctly understood what you mean when you state:
I want to search GCSALLDATA for the Control Number found in GS-RECONCILED. If it's found Update the record.
You can use a simple update query to update records in GCSALLDATA for which there is a matching Control Number in GS-RECONCILED, for example:
update GCSALLDATA t1 inner join GS-RECONCILED t2 on t1.[Control Number] = t2.[Control Number]
set t1.NotInDevTrack = True
Assuming that you wish to set the NotInDevTrack field to True (Yes) for those numbers which match.

Related

Can I use DCount to populate a textbox with criteria from a different table?

I have been having much trouble with this problem. I have a table in Access called "Import" that I import records to. Each record has a facility name that corresponds to a "region" from a different table called "COID_Lookup". I'm trying to get a count of records in the "Import" table based on criteria from the "COID_Lookup" table. Is this possible? Also, I have a query that already does this perfectly but I understand a textbox value cannot be based on a query.
This is what I've tried =DCount("Facility","tblImport","tblCOID_Lookup.Region = 'Midwest'")
My output is #Error in the textbox that blinks as if it is caught in an endless loop.
The query I have, Midwest_Count, works as expected, but I don't know how to put that in the expression. I have tried to look it up but the answers don't make sense to me. I'm sorry.
The solution I used is a DLookup of the query I had that worked.
=DLookUp("CountOfFacility","qryMidwest_Count")
This is the query.
SELECT Count([tblImport].Facility) AS CountOfFacility FROM tblCOID_Lookup INNER JOIN tblImport ON tblCOID_Lookup.[Facility] = tblImport.Facility WHERE (((tblCOID_Lookup.Region)="Midwest"));
Yes, you can do that, as DCount will accept an SQL criteria value:
=DCount("*", "tblImport", "[Facility] = (Select [Facility Name] From tblCOID_Lookup Where [Region] = 'Midwest')")

append access table based on presence of a character

I have an Access table "results" that contains assay lab results for different analytes. I have a second table "results_final" that will contain all the cleaned up values. Cleaning the results occurs in scenarios such as different labs providing results in different ways when analytes are not detected, for example with the use of the "<0.0001" (less than) others use an "X" and so forth. I want to appended the results_final table so that the results for analytes that are detected are kept the same and those that are not detected are changed to "-999999" so that they are consistent (and numerical) in the "results_final" table. This might occur across multiple cells for multiple records.
I have use a simple query to identify records that would qualify such as
'''select * From results where result_value '<' '''
Where I am stuck is using the append to update the 'Results_final' table to insert the desired value.
This will be an ongoing requirement so ideally I need to be able to repeat/automate the process easily.
What is the best approach to change/append all the records that meet the specific requirements?
I would probably just update field in same table. Something like:
CurrentDb.Execute "UPDATE table SET field1=-999999 " & _
" WHERE field0 IN('<0.0001', 'X', 'XX', '-0.0001')"
Or
CurrentDb.Execute "UPDATE table SET field1=-999999 " & _
" WHERE Not IsNumeric(field0)"
Or
CurrentDb.Execute "UPDATE table Set field1=IIf(IsNumeric(field0), field0, -999999) WHERE field1 Is Null"

Access Junction Table SQL

First and foremost, thank you for your time in reviewing my post and offering your advice. I have a database with the following schema:
tblPR:
PRID
Title
Amount
Status
tblProgram:
ProgramID
ProgramCode
ProgramTitle
tblFund:
FundCodeID
FundCode
FundTitle
There’s a many to many relationship between the previous two tables. Therefore, I setup a junction table.
tblPrgFund:
ProgramCode
ProgramTitle
FundCode
FundTitle
(These are lookup fields to their respective tables so I believe Access pulls their key as well which would be the ids)
Now main form is set to tblPR and subform is set to tblPrgFund. There are two comboboxes in subform for FundTitle and ProgramTitle respectively. Requirement is to filter ProgramTitle based upon FundTitle that is selected by user.
To do this I tried the following SQL command in VBA for gotfocus event of program title combo box. But it is only displaying ProgramID and not ProgramTitle. I don’t even think the ids are correct tbh.
"SELECT ProgramID, ProgramTitle FROM tblPrgFund WHERE FundCode = " & Me.FundCode
If I am understanding your dilemma correctly, you want something like this:
"SELECT ProgramTitle FROM tblPrgFund WHERE FundTitle = " & Me.FundCode
OR (if you wanted to use the FundCode, and not the FundTitle)
"SELECT ProgramTitle FROM tblPrgFund WHERE FundCode = " & Me.FundCode
NOTE: This assumes that your FundCode and FundTitle are unique values in your data. But you shouldn't be grabbing the ProgramId, unless your intention is to use it in some way.
That being said, I would highly suggest you use After Update instead of GotFocus as your event for this type of update. Since this is a drop down box, I would assume you don't want the updates to go just from the initial selection of the Combo Box, but rather start after a selection has been made.

Alter Large Block of Records with Data Macro

Several hundred records were incorrectly entered into a table in my Access Database.
A particular ID number includes a two digit leading code that designates the state that record is from; so 01-24586 is a record from the ACT, and 02-55719 is a record from NSW. The incorrect entries have these two switched. I need to replace the first two digits of these records' IDs with the correct code.
To do this, I've tried to write a Named Data Macro that I can call from a regular macro object (so I can double click it in the navigation pane). I've done that, but it doesn't seem to work. My Data Macro (just one of the State fixes) looks like this:
If [State]="NSW" Then
For Each Record In tblCustomer
Where Condition =[State]="NSW"
Alias NSWCust
EditRecord
Alias NSWCust
SetField
Name MyobID
Value = "02-" & Right([MyobID],5)
End EditRecord
End If
When I call it from the other macro, using RunDataMacro it gives me error 3709.
Is this a bad way to go about fixing this? What's wrong with my execution?
Data macros are intended to be used as "triggers" (perform an action when a specific event occurs.
In order to update data, you should use an update query.
Statement would look like this:
UPDATE tblCustomer
SET MyobID = "02-" & Right([MyobID],5)
WHERE [State] = "NSW"

Setting a default value in a blank field in an Access query

This may have been answered elsewhere, but I can't find it!
I'm combining 2 queries from different sources onto a single query for calculation and reporting purposes. Fields which are not common to both sources show as blank if there is no data, and can't then be used in arithmetic.
I want to set a default of zero where blank, instead of having to knife and fork the query into another table and run an update query over all the blank fields.
There's got to be something simpler! HELP!
Give this a try:
SELECT
nz(value1, 0), nz(value2,0), nz(value3,0)
FROM table1 left outer join table2 on table1.column = table2.column
unless your query resembles what John answered with (a cartesian) then his answer is more appropriate.... (cartesians are dangerous if not used correctly... depending on how big the individual tables are allowed to become you can kill an access application by using them)
You'll either want to use Nz() or iif(), depending on whether that "blank" really is just a blank (empty string) or Null.
So your options are:
SELECT Nz(source1.a + source2.b,0) FROM source1, source2
or:
SELECT iif(source1.a + source2.b <> "", source1.a + source2.b, 0) FROM source1, source2