SQL SP To Update 2 Records Using A Value Found In A Completly Seperate Table
Nov 9, 2005
OK this may be a bit of a weird one.
Here are my tables:
Table: Relationship
PK: RelationshipID int (Indexed No Dup)
FK: ContactID int (Indexed No Dup)
Table: Contact
PK: ContactID int (Indexed No Dup)
FK: RelationshipID int (Indexed Dup Allowed)
I also have following Local vars:
@Contact1 int
@Contact2 int
How do I create an UPDATE statement that will read RelationshipID in Relationship where Relationship.ContactID=@Contact1 and use it as the value to update the Contact.RelationshipID for all records in Contact where Contact.ContactID = @Contact1 or @Contact2
Hope this makes sense?
Also please let me know if I should post this in the SQL area instead.
Hoping someone can help me with this DELETE query. I have a Main table that's being updated by a Temp table that's an exact copy of the Main table but with a subset of records.
1) Insert records from Temp table NOT found in the Main table - this query I have worked out below - not tested, but the results look correct.
Need Help Here... 2) Delete Records from the Main that are not found in Temp table with an exception...only DELETE records where certain key fields are matching. i.e. If S.CAD_NAME, lngStoreNumber are a match to what's in the Main table. While Temp table: lngStoreNumber - CAD_NAME - lngcomponentSerial 1 - "CHK" - a 1 - "STK" - a 2 - "CHK" - a
Main table lngStoreNumber - CAD_NAME - lngcomponentSerial 1 - "CHK" - a - LEAVE (EXISTS In Both Tables) 1 - "CHK" - b - DELETE (lngStoreNumber & CAD_NAME composite Found /lngcomponentSerial NOT Found in Temp) 1 - "STK" - a - LEAVE (EXISTS In Both Tables) 1 - "RMM" - a - LEAVE (lngStoreNumber & CAD_NAME NOT Found in Temp) 2 - "STK" - a - LEAVE (lngStoreNumber & CAD_NAME NOT Found in Temp) 2 - "CHK" - b - DELETE (lngStoreNumber & CAD_NAME composite Found/lngcomponentSerial NOT Found in Temp) 3 - "CHK" - a - LEAVE (lngStoreNumber = 3 Not in Temp table Subset)
Rule: Only delete the records for a particular CAD_NAME and lngStoreNumber from the Main table leaving all other CAD_NAME/lngStoreNumbers.
I'm running these updates in batches of lngStoreNumber. So the Temp table will only contain subsets of what's to be deleted from the Main table thus the need to link on the key fields only NOT to delete a Subset of lngStoreNumber/CAD_NAME. I think I've tried every possible query that doesn't work.
Here is query #1 to insert records missing from the Main table that exist in the Temp table. I think what I need is a variation of this??? SELECT D.* FROM Main AS S RIGHT JOIN Temp AS D ON (S.CAD_NAME=D.CAD_NAME) AND (S.lngcomponentSerial=D.lngcomponentSerial) AND (S.lngStoreNumber=D.lngStoreNumber) WHERE S.lngcomponentSerial is null AND S.CAD_NAME is null AND S.lngStoreNumber is null;
I am trying to create a page that can display 4 different records from a database, each one in a different div container, for a news page.
I have an access database containing news records - headline, news and date. Each record also has its own auto-numbered ID.
So far I have managed to get the asp page to display all of the records in a list by using:
recordset.movenext Loop
and I have managed to get just 1 displayed by using:
SQL = "SELECT * FROM tblNews WHERE ID=1" and changing the ID number.
But I can't seem to find a way to display each in a different div container on the same page. Can anyone help please? I think this is really obvious but I just can't see it!
Hey all, For securing a database, is it possible to set a certain password for users to view just one table? like i've lots of tables in my d.b that i plan to use a workgroup file for different users to view it, but there's one that i only want certain people to access...can this be done at all??
I'm making a table with a Yes/No choice many fields which represent subscriptions.
The problem is that the subscriptions available will need to be changed fairly often - will this mean the table and the form associated with it need to be redesigned?
If so, I don't know how to make fields that change depending on a different table.
If this can't be done, I'd be interested to find out how else I could go about this.
I want to be able to find the last value entered into a table from a form that is not based on that table. How do I go about this?
What I am trying to do is produce a 'purchase order number system' that adds 1 onto the previous number, but this purchase order can be generated on many forms and for many reasons across the database. I do not want to use autonumber.
I have been trying to figure out comboboxes and lookup tables on and off for two weeks now and am completly lost. I have read the tutorial on cascading combos. I have downloaded and studied the the autofill db example from Pat Hartman. I have searched and read the threads and am still having a problem. I have several lookup tables. I will use one as example. This a database for my lawfirm. We get a lot of referrals from other attorneys. I have a lookup table with the name, address and phone of all attorneys. I have a combobox on my clientinfo form that looks up the atty info . I put the attorneyid field in my clientinfo table as FK and linked my clientinfo table to the attorney table thru this field. All this did was generate an error message that I could not save my record because there was no corresponding Atty record. I know this should be so simple. Can someone please help me? What do I with data I have lokked yup from a lookup table and how do I link it to the tables that will need it later on? Thank you in advance.
Ok, so I've inherited a db that was created in 97 and I had to convert to 2000. I've battled through all the DAO code issues and the new db works like a charm. Almost. It's an equipment tracking db in which many fields are tracked and updated. The action in question updates those fields in one shot. example a piece comes back from repair and I want to place it back in stock, change status from "maint" to "rfi", and update dates. It also works the opposite way. But it's the same thing. Regardless, when I get to the point of updating I get the "Item is not found in this collection". So now I'm stumped because I'm not sure what I need to add to make this work. Here's the code (note, I removed the .edit lines. Didn't change anything) One question is, what line in this code allows the item to be found? And why won't it find the item in my 2000 version? Again, I get no errors in the code, I just can't seem to connect this update to my db (I guess that's what I'm saying):
Private Sub Command21_Click() On Error GoTo Err_Command21_Click Dim Msg As String Dim Response Dim stDocName As String Dim db As Database, rs As Recordset Dim strSerial As Integer Dim strRepSerial As Integer
Msg = "This will update this device's Status. Are you sure you wish to continue?"
Response = MsgBox(Msg, vbYesNo + vbExclamation)
If Response = vbNo Then Exit Sub Else If Me!Status = "Maint" Then Dim stDocName2 As String Dim stLinkCriteria As String stDocName2 = "frmRecertDate" stLinkCriteria = "[Serial#]=" & Me![Serial#] DoCmd.OpenForm stDocName2, , , stLinkCriteria End If DoCmd.SetWarnings False
'get Serial# of device selected for return to stock strSerial = [Serial#] Set db = DBEngine.Workspaces(0).Databases(0) ' Get current database. Set rs = db![Total Device Listing].OpenRecordset() ' Open table. rs.Index = "PrimaryKey" ' Set index." rs.Seek "=", strSerial
If Me!Status = "IT/Return" Then
DoCmd.SetWarnings False DoCmd.OpenQuery "qryClearRepl" DoCmd.SetWarnings True With rs .Edit !devicecReturned = True !Status = "RCR" !Recall = Null !Location = "holding" !Acct = "123421" rs.Update Refresh rs.Close End With Else With rs .Edit !deviceReturned = True !Status = "RFI" !Recall = Null !Location = "stock" !Acct = "234234" rs.Update Refresh rs.Close End With End If End If Exit_Command21_Click: Exit Sub Err_Command21_Click: MsgBox Err.Description Resume Exit_Command21_Click End Sub
I have a form setup that has 5 combo boxes where a user will select one item from each combo box. Each combo box is from a differant table. Once this is done I want to be able to append or update the results into a new table I created. My problem is I don't know how to submit the data from the combo boxes to the new table. Can anyone help me with this?
I am on Access 2007, and know very basic stuffs to create tables, queries, and form search. I have just successfully completed a search form filtered with a combobox. Also I have a built-in subform within the main form to display other results as well. The display results are based on one complex query (relational query). Now I need to add a command button that would take me to another form to update the current record found.Quick on the design:
- When I search a subject in a main form(subject lists in the combobox), it would populate results below in the main form and also subform would populate other results as well.
1. Add a command button so can take me to new form, but would need to have the current record populated. 2. Once updated, then how do I save it?
Hey everyone, Even though I'm going to feel like a moron for not knowing this I'm hoping there is a simple answer. I'm far from a master at access, and seem to be having a little trouble when it come to formatting the output of my query. I have a database made the keeps track of about 200 hundred crime reports. I want to be able to do a query that will output the 15 possible crimes as rows, and then have 4 columns which are the locations of where the crimes happened. And then for the results have how many of crime where commited there. Pretty much in an excel format (see below example). However there are some crimes that have never happened. So there are no records of them. Unfortunatly when i do my query since there are no records, they are not even listed. I need them to list the crime, and place zeros if there are none. Since this probably isn't to clear I'll put a little example below.
Data:
Robbery: 4 on campus, 5 off campus, 6 in city, 0 in apartments Rape: 0 on cmpus, 3 off campus, 4 in city, 0 in apartments murder: 0 on campus 0 off campus, o in city, 0 in apartments
I have a database that holds hardware data. It's working fine, but there's a slight itch I'd like to scratch and I'm not sure how.
Very simply, I have a table listing printers. I have a search form with combo boxes called 'cboRoom' and 'cboDepartment'. The room and department fields in the printers table are lookups to a room table and a departments table respectivley.
The search form works fine by using the combo boxes to select a room and/or department, click search, and a query is run using the combo box selections as parameters. The query is also made to show all records if the combo boxes contain null. A form is then displayed with the query results.
Say for example, we have a room called B24. If B24 is selected in the combo box and the query run, I want a popup to appear that says 'No data with these search parameters' if there are no records containing 'B24'.
The search button (which is actually a label for design reasons) on the search form currently does nothing more than this;
Private Sub lblSearch_Click() DoCmd.OpenForm "frmPrinters", acNormal DoCmd.Close acForm, "frmPrinterSearch" End Sub
'frmPrinters' is obviously using the query (qryPrinterSearch) as its recordsource. Obviously, the popup needs to appear as soon as the query has been run, but I'm not sure what code to use or where to put it...
I know I need some sort of (pseudo)
If frmPrinters.cboRooms Is Null MsgBox "No Data" Close frmPrinters Open frmPrinterSearch End If
Something like that. But obviously I need to do it for both cboRooms and cboDepartments, after the form has attempted to populate itself with data from the query (otherwise it return null values anyway I guess).
Any help with the code and where to put it much appreciated.
A form i have gains its records from a query. I know that the number of records found is displayed down the bottom, but is it possible to have a text box displaying this, so i can choose where on the form i have it?
I am using a query to search for records and I'd like there to be a message box that pops up on the search page if there are no records found (so the query is empty). I'm guessing there is a simple solution since I think I just need an "If" statement checking to see if a field in the query is null or not. However, I'm not familiar with Access code and what I've tried so far does not work. Any help is greatly appreciated! p.s. I am using Microsoft Access 1997...old school...
Code: SELECT Table1.[Material Key], Table1.[Material Name], Table1.[Info] FROM Table1 INNER JOIN search_MaterialKeys ON [Material Key] = search_MaterialKeys.[MaterialKeySearch];
So at the moment, users paste material numbers in the search_materialKeys table and the query is created with the necessary information. If the number they paste into the search_materialKeys table is not found in Table1 then the query doesnt return anything. I need the query to list all the entered values in the temptable in the query so that users know which of their inputs were not found. Is there any way to do this?
MS Access 2013: I have two database tables as below:
tbl1_MainDB --- It has a field named as "City" where I get huge data for some city names. Sometimes This field may have some unknown/new names which are not listed in our 2nd table ("tbl2_RefrDB")
tbl2_RefrDB --- It's a reference table which has raw names for cities, and then standard names of their city and state in another fields.
Target --- I want to create a VBA prorgram (Sql query) which can look from tbl1_MainDB.[City] to tbl2_RefrDB.[Raw_City] field, and if found then pick the "Standard_State" and "Standard_City" record values from there, and update into the 1st table "tbl1_MainDB".
...if not found in "tbl2_RefrDB" table, then user can be informed & ask for updating the new/unmatched city record as a new record in this table.
The following is code to open a form and records with a specific date as input by the user:
Private Sub cmdDisplayCovers_Click() On Error GoTo Err_cmdDisplayCovers_Click Dim stDocName As String Dim stLinkCriteria As String stDocName = "frmInventory" stLinkCriteria = "[tblCovers].[Date] = [Forms]![frmCoversByDate]![txtDate]" DoCmd.OpenForm stDocName, , , stLinkCriteria etc. etc.
generate a message to user if no records match the input and then return to the input form. Currently, the form opens even with no records.
Any way to build something into a sub-query that says 'if no records are found, return '0' or some other string'?
Otherwise is there a way to make a master query ignore sub-queries if they return no records?
Allow me to explain in more detail: I have a series of sub-queries, most of these take the sum of several fields from a number of different tables, and I have a main query which combines all of these, to be used as the basis of a summary report.
These queries aren't a problem, but I have a few other essential queries which take the modal (most common....) entry for fields which aren't numerical. So I can't use the sum function.
Now, if all the sub-queries are working then so does the main query, however if one of them fails to find a result, then none of them show up in the main query. I don't know why.
The issue is that depending on the date range selected, some of the tables targeted by the sub-queries don't have any records at all, so when they are run they return nothing. The sum queries can handle this since they just return 0, but those searching for modal records just find nothing (not 0's and not null fields, just blank across all rows).
Here's an example of my sql statement for the modal queries.
Code: SELECT TOP 1 Trends.Trend AS ModeTboxTalk, "1" AS [Key] FROM Trends INNER JOIN [Toolbox Talks] ON Trends.TrendID = [Toolbox Talks].TrendID GROUP BY Trends.Trend, [Toolbox Talks].TrendID, [Toolbox Talks].TalkDate
HAVING ((([Toolbox Talks].TalkDate)<=[Forms]![WeeklyReportSelect]![WeekBox] And ([Toolbox Talks].TalkDate)>[Forms]![WeeklyReportSelect]![WeekBox]-7) AND (([Toolbox Talks].SiteID)=[Forms]![WeeklyReportSelect]![SiteBox]))
ORDER BY Count([Toolbox Talks].TrendID) DESC;
- FYI the weekly select form is where users select the week and site they want to report against. So it would be really peachy if I could tell the above to say something like 'no trend this week' if indeed there were no records.
I have a table called tblCompanies. When a company acquires another company, I need a method by which the acquired company's CompanyID (PK) can be updated to the new company's CompanyID (PK). I also need to be able to update all related CompanyIDs (FKs) to the new value in related tables.
In cases in which the new company does not have an existing record, there is no problem: the company name simply gets changed to the new company and the existing CompanyID is maintained. I then use an audit table and Track Changes function to keep track of the company name data and a union query to keep the old names in the selection lists.
The problem is when both companies already have existing records in the table.
So, let's say I have records for Company A and Company B. Company A merges with Company B and Company B is now the main record. What is the best, simplest and easiest way to update the CompanyID (PK) from A to B and change the CompanyID (FK) to the new value in all related tables?
I am envisioning a pop-up form that directs the user to select the new company and then an update query happens behind the scenes... but exactly how does the criteria for the update query get selected and how do all the related tables get updated? My vba skills are pretty basic, will I need extensive coding to do something like this?
I am looking for a way to update certain records in a table with an incremented value of +1 each time.
For example, TBL_MAIN will have fields REF_NO and INCREMENT_VALUE. In field REF_NO, the same record may appear more than once and the INCREMENT_VALUE field needs to update by +1 each time. I have attached a simple snapshot of the table. In this example, I would like ref ABC123 have it's INCREMENT_VALUE increased by +1 each time following on from the last incremented value of 12460. I believe this may involve DMAX function, For Next loop and possible DAO recordset code but don't really know where to start!
This may seem a really dumb question but I just can not get my head around the best way of solving it.
I have two tables orders and orders_tmp the table orders is created form the orders_tmp table. The order is created by a single field createorder (yes/no)from the order_tmp table. I run an append query to copy the records into the orders table. this woks fine.
The problem I am trying to solve is how do I now go back and change the order for what ever reason. I need the order_tmp table to now also include my existing order.
Code: Dim rs As Object Set rs = Me.Recordset.Clone rs.FindFirst "[OwnerID] = '" & Me![lstUsers] & "'" Me.Bookmark = rs.Bookmark <---- error is here for the datatype mismatch or No record found
I need to use an update query to count the number of selected records and then place this number in another table using an update query. The table has a field with a 1 in that field for all records to do the counting. I know I can use forms and subforms, but the data needed is too massive to be practical. Thanks Abe