Let us say that we have an SQL database with a contacts table that has 5 fields; contactID, title, firstName, middleName, and lastName. On the front end we have a profile page with an update button.
Let us also say that we want to allow the user to update any given field without having to also enter data in the other fields (a last name change, for example.). Is there a 'simple' way to allow this?
My solution is to add a bit field to each property and add a series of condition statements to generate the UPDATE statement based on the bit field.
It is ugly and seems inefficient. Any advice?
The best way is to keep data retrieved from database in all fields and update all the fields. If user changes the value, it will be updated to new value. Otherwise it will stay the same old value.
Suppose that you retrieved all the values from database and on click of edit button, they are editable(textboxes) with old value already present in them. Then write a query like
update contacts set title =#title, firstName=#firstName, middleName=#middleName, lastName=#lastName where contactID=#ContactID
Related
I have problems with my records within my database, so I have a template with about 260,000 records and for each record they have 3 identification columns to determine what time period the record is from and location: one for year, one for month, and one for region. Then the information for identifying the specific item is TagName, and Description. The Problem I am having is when someone entered data into this database they entered different description for the same device, I know this because the tag name is the same. Can I write code that will go through the data base find the items with the same tag name and use one of the descriptions to replace the ones that are different to have a more uniform database. Also some devices do not have tag names so we would want to avoid the "" Case.
Also moving forward into the future I have added more columns to the database to allow for more information to be retrieved, is there a way that I can back fill the data to older records once I know that they have the same tag name and Description once the database is cleaned up? Thanks in advance for the information it is much appreciated.
I assume that this will have to be done with VBA of some sort to modify records by looking for the first record with that description and using a variable to assign that description to all the other items with the same tag name? I just am not sure of the correct VBA syntax to go about this. I assume a similar method would be used for the backfilling process?
Your question is rather broad and multifaceted, so I'll answer key parts in steps:
The Problem I am having is when someone entered data into this
database they entered different description for the same device, I
know this because the tag name is the same.
While you could fix up those inconsistencies easily enough with a bit of SQL code, it would be better to avoid those inconsistencies being possible in the first place:
Create a new table, let's call it 'Tags', with TagName and TagDescription fields, and with TagName set as the primary key. Ensure both fields have their Required setting to True and Allow Zero Length to False.
Populate this new table with all possible tags - you can do this with a one-off 'append query' in Access jargon (INSERT INTO statement in SQL).
Delete the tag description column from the main table.
Go into the Relationships view and add a one-to-many relation between the two tables, linking the TagName field in the main table to the TagName field in the Tags table.
As required, create a query that aggregates data from the two tables.
Also some devices do not have tag names so we would want to avoid the
"" Case.
In Access, the concept of an empty string ("") is different from the concept of a true blank or 'null'. As such, it would be a good idea to replace all empty strings (if there are any) with nulls -
UPDATE MyTable SET TagName = Null WHERE TagName = '';
You can then set the TagName field's Allow Zero Length property to False in the table designer.
Also moving forward into the future I have added more columns to the
database to allow for more information to be retrieved
Think less in terms of more columns than more tables.
I assume that this will have to be done with VBA of some sort to modify records
Either VBA, SQL, or the Access query designers (which create SQL code behind the scenes). In terms of being able to crunch through data the quickest, SQL is best, though pure VBA (and in particular, using the DAO object library) can be easier to understand and follow.
What I have here is a table with some text fields. I basically put in the name of some students into a table. I got their info from forms that teachers had sent me. When I put their names into the database, I just put the names in based on which form was at the top. Does access track the actual time that a field was put in in such a way that I could sort my text fields by that so that when I give a list of what I just did to someone else, they won't have to sort through the stack of papers to make sure all of the papers are there?
I should add that I need to track this for an individual field, not just for the record. For instance, let me take this one piece of paper I have. When I get it from an outside agency, I then put it into the database with the student's name and some info. I then need to send it to the teachers for them to sign and send back to me. I am on the second part so the record had already been created, but I want to find the date that I entered the Date_signed field basically.
In this example, you set the default value of the dtmEntered field to Now()
p.s. Same answer as HugoLemos but with a pic :)
You can use a Date/Time field with its default value = Now() to store the time each record is created, as already suggested. That approach works fine when only one user can be adding new records, which sounds like your situation.
If you wish to also store the time an existing record is changed, you can do that from a form's before update event.
This example assumes a text box named txtLast_change which is bound to a Date/Time field in the form's record source. The text box does not have to be visible to the user for this to work.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.txtLast_change = Now()
End Sub
Create a field with a date type and set default value to Now()
As you have discovered, there isn't any way after-the-fact to know when data was entered in to your table, as it's not something that you captured in the first place. As you mentioned in one of your comments, you can determine the order in which you entered new records based on the Id, but knowing when additional data was entered would require more tracking fields.
In the future, you may want to think about a table design along these lines:
Column Name Column Description
ID Record Id
STUDENT_ID Student Id Number
STUDENT_FIRST_NAME Student First Name
STUDENT_LAST_NAME Student Last Name
... Other student info ...
DATE_ENTERED Date/Time entered
DATE_SENT Date/Time sent to teacher
DATE_SIGNED Date/Time signed by teacher
UPDATED_DATE Date/Time record last updated
UPDATED_BY User that made the last update
If it's possible, you could always edit your table and add these datestamp columns. You'd have to allow for null values, as the previous entries wouldn't have a date/time value for some of them, but it would let you track future entries.
I am helping a small school with their database which I created three years ago and that works fine.
I am now creating a attendance register as a new feature to it and am struggling with the update query fields.
I have two tables and one append query which works well with the form I have created. I have combobox to filter the class to check the register which works well. The problem I am faced with is when running the query, it updates all 180 students and not the ones filtered per class.
I need to append the data only for the class selected. Thank You
Please help.
Harry
It seems that you wish to only update or append the rows that match the combobox on your form. You can refer to an open form in a query in MS Access. With your append query in design view, under the field that you want to match and type in Form! followed by the name of your form, another dot or exclamation, and the name of your combo, say:
Forms!Form1!Combo0
In SQL View an append query would look something like:
INSERT INTO Table1 ( AText )
SELECT Table2.AText
FROM Table2
WHERE Table1.ID=[Forms]![Form1]![Combo0]
Similarly for an update query:
UPDATE Table1b
SET Table1b.ADate = Date()
WHERE Table1b.ID=[Forms]![Form1]![Combo0]
I have a scenario wherein with the following details:
I have a form that contains fields like firstname,middle name,lastname,dob,pin,address for Client 1.
Now this form has to cater more than one client . The problem statement is - different clients may require different number of fields to be displayed on front end. For eg: a 2nd client would want - Country field instead of pin, would not require address on the form.
Now when i submit the form , currently i have a constant query which takes values of - firstname,middle name,lastname,dob,pin,address and inserts it into database for Client 1. I want to develop a query in a way that it is created at runtime and inserts only the values that come from the form into the database..
I hope my problem is clear. Can anyone help?????????????
You need to think about why you are doing this.
It will get hideously complex with just person form, add in more and it will balloon big style.
Dynamically building queries isn't particularly complicated, but it's a lot of code to do it.
Just think about each field you want to add to the query and it's type. What if a property on your person record was an image of them.
Do you have a configuration of the form, is the promary key on the record an auto inc, is it a compound key, do you use defaults, are some fields not null. How are you going to bubble back referential integrity exceptions...
Do the all singing all dancing version and basically you reinvent something like the Access IDE....
Personally I'd have a Person object with a set of Person Properties, they would have name, a value and a boolean to say whether they'd been changed.
Once you have teh list of chnaged properties and beacseu you are in the Person object you know the table is persons, it's keyed by an autoinc bigint, gender is mandatory and defaults to Male...
You have a fighting chance.
Your query should use parameters
So it would be say Insert Persons(FirstName, LastName, PIN) Values(#FirstName,#LastName,#PIN)
Then you'd nip through your changed fields and add parameters with same name, type and value.
As I said you can really go to town with this one, may be it's time for a night in though.
This should mean that some fields in your table like address and pin can be empty, in that case you can do without a dynamic query. Just collect all the inputs from your form and insert them into your table. Those form fields that were left empty due to different user needs will consequently have their corresponding field in your table empty. So just list all the needed fields in your table and all the possible input from your form in your insertion query.
So I have a form that I want the user to use to update multiple tables with. I do not want the form to be bound to the tables, as I would rather do this with VBA (I think, I don't know).
So here is a little table example
UserTable
UserID
First
Middle
Last
Division
Title
EquipTable
EquipID
AssignedLaptop
Model
Barcode
SoftTable
SoftID
SoftwareName
License#
Custom (running sum to calculate how many licenses are left....another question)
ExchangeTable
UserID
SoftID
EquipID
So that is how I set the tables up. All these text/cmb boxes exist on one table, and I would like to be able to save this data from this one form to all relative tables with a button click.
So can anyone help me with the following:
If I have this unbound form populate after a Create New button click, how then can I tell hidden text boxes (that ideally are to contain the " "ID numbers), to populate new ID numbers (auto-numbers), for each of the tables, in them, so that I can assign an UPDATE SQL statement to a SAVE button click, in the VB to save the data?
Could I set it up so that I just the "exchange table" (no idea why i named it that) populates the ID numbers for the other tables, instead of visa versa?
Am I going about this the wrong way
EDIT:
I was just trying to give you an example to see if what I was trying to do is possible with multiple tables (ultimately multiple Keys) on one form, and if so how does it differ from doing it with one form/table: I use the unbound approach alot because of the forms I have to build, and the need to constantly pas parameters from one form to the next in the VBA. I think it is faster to code it myself? So for this concept I always have a hidden text box on the form that usually has the IDnumber of the relative table. So to click save would just require a simple SQL = * WHERE Tbl_ID = frmID kinda idea.
All I was wondering were these questions?
When you run an INSERT, does the ID number need to be present in the STATEMENT or will access just assign the next relative one for you when you execute?
If not
Considering the method above, how can i determine this number (ID I need to use) myself, with code?
Another question? How do you defeat the mouse wheel scroll through records function on a bound form?
I see what you are saying, wording was strange on this but I do see where you are goign with this.
This is what you have:
You have an unbound form that is not linked to any table
This unbound form that is in no way linked to any table will be designed by you by adding some text boxes and also combo boxes and buttons
Within these textboxes you have some textboxes that are going to store the ID (The key value) of the row. So that when you do a save the ID number is set back to the text box
Once the ID number is set to this hidden field you can then issue updates to all tables that need this ID number
Does that sound right? If so it is very simple. All you have to do is once you click the "Create Button" perform the insert, something to this effect:
' Command to Insert Records.
Dim cmdInsert As New OleDbCommand()
cmdInsert.CommandText = "INSERT INTO AutoIncrementTest (Description) VALUES (?)"
cmdInsert.Connection = cnJetDB
cmdInsert.Parameters.Add(New OleDbParameter("Description", OleDbType.VarChar, 40, "Description"))
oleDa.InsertCommand = cmdInsert
Then you issue another command to grab the IDENTITY back, namely your ID:
' Create another command to get IDENTITY value.
cmdGetIdentity = New OleDbCommand()
cmdGetIdentity.CommandText = "SELECT ##IDENTITY"
cmdGetIdentity.Connection = cnJetDB
You can always get an identity with SELECT ##IDENTITY
So in your code behind you set the textbox to the value of SELECT ##IDENTITY.
I haven't done access in over 5 years but I remember doing something like this. Once you have this identity you place it in your hidden text box. This will help you perform your UPDATES that use this identity.
Here is a good place to start: http://support.microsoft.com/kb/815629
Here is another great article that may help you: http://support.microsoft.com/kb/815629