473,801 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bound form - insert or update based on key value?

geolemon
39 New Member
This should be an easy one for anyone with basic forms experience (I'd hate for you guys to be bored!) :D

I have a simple form, bound to a single table.

I'd *like* the form to work this way:
  • The form should open and be all blank.
  • If the user selects the "Job Order Number" control and picks an existing Job Order Number, all the controls on the form will position to that job order.
    When the user manipulates the data in other form controls, the existing Job Order record is updated.
  • If the user instead types in a new Job Order Number, and inputs/selects data into the other controls, this is inserted as a new Job Order record into the table.

On my 'form so far'...
I used the wizard to add one control with the "use the value of this control to select the value of other controls on my form" (that's not a quote - that's just the concept, I don't remember the exact wording... third option on the first Wizard page ;) ). That's the control for the key value in the table - "job order number".
...and that part works. I select a Job Order, the rest of the controls position to that record - and I can update it. Nice.

But, I can't "insert' using this form! I don't have a blank value to begin with - the form automatically positions to the first record. And, if I type a new value over the selected Job Order Number, it updates that record, changing the record to a new Job Order Number. Bad.

I'm guessing this is a simple, "forms 101" type of question - my apologies for that - at least I'm hoping it's a simple issue!
Sep 7 '08
17 6611
NeoPa
32,579 Recognized Expert Moderator MVP
Before I leave you in what appear to be the fully-capable hands of cm5th, I will just explain that the Job Order Number control (you hadn't been very clear with control names at that point in the thread) that allows you to select the item you want (or add a new record) should NOT be bound and should be separate from the control where the current Job Order Number data is displayed on the form (which, of course, should be bound).

It is possible to do it with a single (bound) control, but even with clever coding it does cut down your options, and is actually not very intuitive for most users.

Feel free to go this route of course, but it wouldn't be my recommendation. A separate, unbound ComboBox control does the job for you quickly, fairly easily, and in a fashion which is immediately clear to anyone using it.
Sep 9 '08 #11
cm5th
11 New Member

Does the "ADD" button simply let the user press it and position the form to the blank record, essentially? That sounds nice actually. In fact, that sounds like a nice addition regardless of whether I've already coded it the other way. ;-)
The ADD RECORD button opens up a new record for a bound form if the primary key is defined as AutoNumber and displays the empty (except for the primary key - JobOrderNumber) .

In your case, I'd open a recordset by selecting all records in tblJobOrder, then use ADDNEW, UPDATE to add a record, input the field values, and update the recordset. e.g. ...
Expand|Select|Wrap|Line Numbers
  1. Set rst1 = New ADODB.Recordset
  2.       With rst1
  3.         Set .ActiveConnection = CurrentProject.Connection
  4.         .LockType = adLockOptimistic
  5.         .CursorType = adOpenKeyset
  6.         .Open "SELECT * FROM tblJobOrder"
  7.         .AddNew
  8.           .Fields("JobOrderNumber") = 'Predetermined Job Order Number"
  9.           .Fields("Customer Name") = Customer Name
  10.           .
  11.           .
  12.         .Update
  13.       End With
  14.       rst1.Close
  15.       Set rst1 = Nothing
Sep 9 '08 #12
geolemon
39 New Member
Before I leave you in what appear to be the fully-capable hands of cm5th, I will just explain that the Job Order Number control (you hadn't been very clear with control names at that point in the thread) that allows you to select the item you want (or add a new record) should NOT be bound and should be separate from the control where the current Job Order Number data is displayed on the form (which, of course, should be bound).

It is possible to do it with a single (bound) control, but even with clever coding it does cut down your options, and is actually not very intuitive for most users.

Feel free to go this route of course, but it wouldn't be my recommendation. A separate, unbound ComboBox control does the job for you quickly, fairly easily, and in a fashion which is immediately clear to anyone using it.
I absolutely *did* step into that pothole, in fact!

What I did first was try to bind the control... you can imagine how I broke it.

To work around the issue of needing that value inserted into my table (particularly as it's the key), what I did was add an invisible text box that was bound to the key column of the table. ...And I created a CustID text box to fix the Customer subform issue I was having before (I mention it as it's workaround is incorporated here too).

Then, I modified the VBA code that positions the other controls based on the value of the unbound control so that I have this logic:

Expand|Select|Wrap|Line Numbers
  1. IF control.value = "<ADD NEW>" then
  2.      store my CustID value into a variable
  3.      DoCmd.GoToRecord... (go to new record)
  4.      set my CustID text box = CustID variable
  5.  
  6. ElseIf  rs.NoMatch
  7.      set bound control = value of unbound control
  8.  
  9. Else
  10.      position other form controls to currently selected record
  11. End If
Any obvious flaws? And thanks for the good suggestions that got me here!
Sep 9 '08 #13
cm5th
11 New Member

To work around the issue of needing that value inserted into my table (particularly as it's the key), what I did was add an invisible text box that was bound to the key column of the table. ...And I created a CustID text box to fix the Customer subform issue I was having before (I mention it as it's workaround is incorporated here too).

Then, I modified the VBA code that positions the other controls based on the value of the unbound control so that I have this logic:

Expand|Select|Wrap|Line Numbers
  1. IF control.value = "<ADD NEW>" then
  2.      store my CustID value into a variable
  3.      DoCmd.GoToRecord... (go to new record)
  4.      set my CustID text box = CustID variable
  5.  
  6. ElseIf  rs.NoMatch
  7.      set bound control = value of unbound control
  8.  
  9. Else
  10.      position other form controls to currently selected record
  11. End If
Any obvious flaws? And thanks for the good suggestions that got me here!
It appears you are indeed using an unbound and visible combo box (as recommended by NeoPa) with the prompt added with the UNION clause. It's not clear to me how you accomplish your Else clause activity, but at this level I see no obvious problem. Pls let us know how you do it if it works.

I have a DB with a form that does something similar. However, I allow users to filter by combo boxes in the parent form and put my bound controls inside a subform. It has been working for adding new records, but I have some issues with helping the user supply a meaningful new value for a text field in my table that requires uniqueness (i.e. the true key). It's possible that I may need to adjust the record add design. So I'm interested in seeing how you do the record add.
Sep 9 '08 #14
NeoPa
32,579 Recognized Expert Moderator MVP
I absolutely *did* step into that pothole, in fact!

What I did first was try to bind the control... you can imagine how I broke it.
...
Any obvious flaws? And thanks for the good suggestions that got me here!
I would only ask why it is so complicated.

You don't explain why you didn't go with the "almost done for you by the wizard" approach outlined above.
Sep 9 '08 #15
NeoPa
32,579 Recognized Expert Moderator MVP
@cm5th

I hope you find something in here to help your situation. If not, feel free to post your own thread and, if you link in the thread address into a post in here (that won't be hijacking) I will make sure to check it out.
Sep 9 '08 #16
geolemon
39 New Member
It appears you are indeed using an unbound and visible combo box (as recommended by NeoPa) with the prompt added with the UNION clause. It's not clear to me how you accomplish your Else clause activity, but at this level I see no obvious problem. Pls let us know how you do it if it works.
Sorry, I skipped some detail-
I *am* using your UNION suggestion - what a perfect way to get a selection like that into the list!

The logic is basically this, in that code:
  • If the user selects <ADD NEW>, the form positions all the controls to the "new record" record - and essentially updates it. But since I'm updating the "new record" record, I'm inserting a record.
  • "Else If" the user just starts typing a Job Order into the control, and it doesn't already exist in the list of existing Job Orders, then we're creating a new record as well using that part number (I may now see a flaw - I need some of the code in the "IF" section).
  • "Else" the Job Order was picked from the list / matches one in the list, and the form controls position to that record.
My only additional complication (to I think answer your question, NeoPa) is that this is actually a subform - the table my subform is bound to has an enforced relationship to a Customer table, which the main form is bound to- so the user selects the Customer, and all Job Orders that correspond are made available in this subform.

...which is why I need to manipulate that Customer ID value - or I throw a referential integrity error. Small hoop to jump through... the Wizard wasn't a help. More like the Wizard of Oz! lol
[/quote]I have a DB with a form that does something similar. However, I allow users to filter by combo boxes in the parent form and put my bound controls inside a subform. It has been working for adding new records, but I have some issues with helping the user supply a meaningful new value for a text field in my table that requires uniqueness (i.e. the true key). It's possible that I may need to adjust the record add design. So I'm interested in seeing how you do the record add.[/quote]
It actually seems to be working fine - the exception being the extra code I think I need to add... I obviously haven't even tested it completely enough to satisfy that IF condition.
So - I WILL get back to you, here!

And thanks to both of you for all the help so far!
Sep 10 '08 #17
NeoPa
32,579 Recognized Expert Moderator MVP
No worries.

I'm a little confused on exactly where you are now, but you can worry about clarifying that later if it becomes necessary.
Sep 10 '08 #18

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

Similar topics

3
2441
by: PAUL EDWARDS | last post by:
I have a windows form that is bound to a datatable. In VB6 I could just update the field contents and it would be updated in the database, however if I update the text property of the control from code it is 50% chance that the update will make it back to the dataset. If I update the dataset instead of the form, it does not show on the form. Is there a method that should be used?
1
15428
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
19
4113
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
14
10146
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought I should give back to the community by posting our findings. Thanks you all for all your help till now by posting problems and their solutions. ~Abhijit
4
2738
by: gj | last post by:
Hi, I'm trying to update a sql database from a web form using text boxes. I'm trying to learn C# on my own so I am at a complete loss. I created my sql connection, data adapter, dataset and data view in the visual studio designer. I'm trying to keep a history of the record so instead of editing the record I insert a new record with my changes. Instead of the changes, it inserts the orginal record. Below is the part of the code. Any help...
25
4077
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
4
4623
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box bound to a table as a lookup, drawing values from another table to populate the available selections. This all worked fine in VB6. I have distilled the problem down to a simple form drawing data from the Northwind database for a representative...
0
2516
by: Mike | last post by:
So here's the situation (.NET 2.0 btw): I have a form, and on this form is a textbox among many other databound controls. The textbox is bound to a field in a data table via the Text property. In this table there are multiple columns that cannot be NULL, which, are bound to other controls (but they're not really important at this time). I create a new row via the currency manager like so: _currencyManager.AddNew() _currentRow =...
8
5283
by: chromis | last post by:
Hi, I'm writing a contacts section for a cms on a website, I've decided to write the section in OO code. So far I have my Contacts object and a page structure I would use for a procedural site. /com/Contacts.cfc <cfcomponent displayname="Contact" output="false" hint="Contact component"> <!--- properties: used for self-documentation ---> <cfproperty name="dsn" displayname="dsn" hint="Contact id (UUID)" type="string" required="false"...
0
9697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10515
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10260
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9100
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6827
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2956
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.