VBA for taking information from text boxes and inserting into table - sql

So I have an input form that I want to use to update a table with certain fields of information. I have the ID of the record automatically coming up in a text box. I have three text boxes that I need to add to the table (excluding the ID) on a button click.
Name
Date
Method
are the field names.
As_Name
As_Date
As_Method
are the text box names
The table name is POC, and the ID is POC_ID (its an autonumber).
So I do not want these objects (text boxes) to be bound to the table because this is a little separate "pop-up" form that comes from a form that the table, and I only want the input to be relative to the POC_ID that is already selected via the original form.
So how do I write the vba for this to 1)check to make sure that records do not already exist....2)update the fields (listed above) with data input from the text boxes(listed above). I want to be able to use this with a button click....
EDIT:
actually it is one table not two; i have two forms that I want to be able to send information to the same table (different information though). this db was already built by someone else and know I have been deamed to take it over.
I need to add a second little pop up form for additional information to be added based on new requirements (there is literally no where for me to place this on the other one). I have already done that, and used a suggested object approach to reference the first forms (from which this second "pop-up" form springs from) to add the relative id fields. Now I have this second little pop up form that just asked three values to be inputted (which are the ones listed above).
I just simply do not know how to link the text box, with a field so that once a user enters in the information, clicks "save" it saves the information to the table relative to the TripID that is there (one mentioned above). the only way I know how to get the text boxes to save data to the table is to use the builder/wizard when I create a new one.
I would like to learn how to link an object (text box, cmb, list) etc on a form, to a table with an "On Click" method so that I can use a save button. Basically that is it!

The OpenForm method of DoCmd allows for several arguments, including Where and Openargs. You can take advantage of these.
However, something seems to be quite wrong with your table design in that you appear to be holding the same information in two tables and for no stated reason. Have you read http://www.r937.com/relational.html?
I would suggest that the design you need probably only includes a numeric field POC_ID that is a foreign key to the main table.

Still not sure I understand your situation, but let me offer the outline of an answer. If my outline is not close enough, please explain where I went astray.
Your parent form, frmParent, has a command button (cmdMoreFields) which opens the child form (frmChild) where you will enter values for 3 additional fields in the record currently displayed in frmParent. After the user enters those values in frmChild (in text box controls named As_Name, As_Date, and As_Method), she will click a command button (cmdSave) to store those values to fields (Name, Date, and Method) in table POC, and close frmChild. Also, frmParent includes a text box (txtPk_field) which holds the value for the primary key (field pk_field in table POC) of the current record.
However, I'm not sure which field/control you're using for txtPk_field, and doubt that value is available if the the current record has not yet been saved. So, I'll suggest this code for the cmdMoreFields "on click" event:
If Me.Dirty Then Me.Dirty = False
DoCmd.OpenForm "frmChild"
In the "on click" event of cmdSave (on frmChild), try code similar to:
Dim strSql As String
strSQL = "UPDATE POC SET [Name] = """ & Me.As_Name & """, [Date] =#" _
& Me.As_Date & "#, Method = """ & Me.As_Method & """ WHERE pk_field = " _
& Forms!frmParent.txtPk_field & ";"
Debug.Print strSql
CurrentDb.Execute strSql, dbFailOnError
DoCmd.Close
If that approach works, consider passing the pk_field value to frmChild with Openargs, as Remou suggested.
Note: I assumed the data type for Name is text, Date is date/time, and Method is text. You will have to change the UPDATE statement delimiters for any fields whose data types differ from my guesses.

Related

Open a form to a record selected in a subform

I want to open a form to the record selected in the subform of a different form. I've tried following what I’ve seen in other posts about this, but I still can't get it to work. I think I'm just missing some very basic detail but this is my first Access database and I can't figure out what it is. Thanks in advance for your assistance.
Details:
F_Detail - (This is a Single Form containing details on a project.)
F_List - (This is a Single Form containing a subform and a button.)
subF_List - (This is the subform contained in F_List. This subform is in Datasheet view)
Project_ID - (This is the primary key contained in subF_List and in F_Detail. It is the common criteria between the two. It is Short Text type.)
subF_List displays row after row of projects. F_Detail displays details about a single project at a time. From F_List I want to select a row in subF_List and then click the button to open F_Detail, and F_Detail will be displaying details of the project whose row was selected in subF_List when I pressed the button.
What I Have for the Button:
On Click > Event Procedure
Private Sub ProjectDetailButton_Click()
DoCmd.OpenForm "F_Detail",,,"Project_ID = " & Me.Project_ID
End Sub
Upon clicking the button, I get an error saying, "Compile error: Method or data member not found" and it highlights the .Project_ID at the end of my code.
I don't know what I'm doing wrong and would appreciate any help you can lend. Please let me know if I've left out any needed detail.
Use apostrophe delimiters for text field parameters. Use # for date/time, nothing for number.
"Project_ID = '" & Me.Project_ID & "'"
Me is alias for form or report the code is behind. To reference a field in its RecordSource:
Me!Project_ID
Code on main form referencing field on subform must reference through subform container control. I always name container different from object it holds, such as ctrProjects:
Me.ctrProjects!Project_ID
I name controls different from fields they are bound to, such as tbxProject:
Me.ctrProjects.Form.tbxProject

Access VBA: Attachment image won't load in form

I have a table that contains items, their location, and an attachment photo of the item. The location is set up by row #-shelves #-shelf #. For example 2-3-4 would be in the second row on the third set of shelves on the fourth shelf down. I have another form that allows users to search by shelf. Basically, they enter the first two numbers and I do this.
DoCmd.OpenForm "fReview", , , "Location Like '" & locStr & "*'"
Where 'locStr' is the string containing 2-3 (the first two numbers), and when the form fReview opens, it should show all items on all shelves of the second row, third shelf unit.
This part works just fine. However, when fReview opens, the image part of the attachment field that I put on the form stays blank. The rest of the fields populate with no problems. When I close the form and open it again normally (with no Where condition), the image works perfectly (although it does blink a few times like it is refreshing multiple times which is weird). The form is linked to the table, so it should all be directly linked, so I don't know what's happening.
So, I created a brand new tiny database to test some things, and figured out my problem. Thought I'd share it, just in case somebody eventually had the same problem I did. It was actually pretty simple.
When I added the attachment field to my form, I just dragged it over from the "Add Existing Fields" bar. This linked the Attachment.FileData as the control source for the field, which seemed ok. Turns out that that isn't right for some reason. If you change the control source to simply be the field name, it works.

MSACCESS - Closing Form edits 1st entry

I have table with 3 Columns. It has a AutoNumber Primary ID (cboID), A Text Column (txtLocation), and a Number Column (txtCount).
From this table, I made a form that has a combobox and two text fields. When the combobox is clicked, it shows a list of all the cboID's. After selecting one of the choices, it autopopulates the two other text fields with it's respective Location and Count info. I also have an update statement/save button that allows the user to edit one of the entries, press save, and update one or both the fields.
Here's the statement I have.
Private Sub cmdUpdate_Click()
Dim strSQL As String
strSQL = "UPDATE aDuh SET Location = '" & Me.txtLocation & "', Count = '" & Me.txtCount & "' WHERE ID = '" & Me.cboID & "'"
DoCmd.RunSQL (strSQL)
End Sub
The updating has no problems, but the form updates the 1st entry when I close it. Lets say I just finished editing the third entry and I'm done editing. I want to close the form and do something else. However, I close the form (I've tried putting a button and just closing the tab) and the 1st entry in the table is updated with the third entry. So basically, my last edited entry edits properly, but when the form is closed, it also updates the 1st record. Any idea of how to fix this? (If this isn't clear please let me know and I'll try to clarify)
I'm thinking of writing a statement for the On Close event but I'm not sure what to put.
EDIT: Extra Info
Right now, this table only has three records. In the form, when I click the combobox, it has the drop down list of the three records. Lets say I clicked the second one. After clicking it, the Location field is populated with the related info and same with the count field. Now lets say the current value in the count field is "9" and I want to change it to 4. I type in 4, press update and I'm prompted with a box saying "are you sure you want to update 1 row". I press yes, and it updates. Updating works fine, its just when I close. I'll give two examples
1) I just edited the second record, when I close, it doesnt prompt anything, it just closes smoothly, but when I check the information, the first and second record are now the exact same.
2) Lets say I just edited the first record. When I close it, a window pops up saying "Write Conflict, This record has been changed by another user since you started editing it. If you save the record, you will overwrite the changes". It then has three options, Save Record, Copy to Clipboard and Drop Changes.
Oh and how i'm making it autopopulate, I have the code:
Private Sub cboID_Change()
Me.txtLocation = Me.cboID.Column(1)
Me.txtCount = Me.cboID.Column(2)
End Sub
Hopefully this clarifies a bit more. :(

How to work around Access VBA error 3188

Having converted a number of fields in a table tblSource to Rich Text memos, I'm getting an error 3188 in the following circumstances.
Main form has a subform open (frmSource) bound to qrySource. qrySource pulls in some fields from tblSource and adds a calculated field which concatenates the (newly-minted) rich-text memo fields (SD1 to SD20) so that the result can be displayed in a single text box on frmSource called Citation.
If the user wishes to edit SD1 to SD20, they double-click on the Citation field and a modal form frmCitation opens up displaying the SD fields for editing. frmCitation is bound to qryCitation which pulls the SD fields and a couple of others out of tblSource. When finished, they close frmCitation. When SD1 etc were text fields, the tblSource record was updated successfully. However, now they're memo fields, I'm hitting VBA error 3188 ("Could not update; currently locked by another session on this machine.").
Searching on the Internet suggests that this is a common issue with Rich Text memos when a memo size exceeds 2k (limitation possibly due to Access edit buffer size?), so I'm looking for ways to work around it.
One option would be be to split tblSource into two tables tblSource and tblCItation with a one-to-one relationship between them, then base qrySource on tblSource and qryCitation on tblCitation, but that's fairly major surgery with knock-on effects in a number of other places in the application.
Another option is to limit the size of all the memo fields on this form (as per Rich text input into limited length text field in Access 2010), but there's one field for which that wouldn't be acceptable to the users.
Is there another technique I could explore?
Per the following MS Link: http://answers.microsoft.com/en-us/office/forum/office_2007-access/memo-field-could-not-update-currently-locked-by/d5c8163a-7ce5-484f-80d4-98c1a8c92160
near the bottom, they suggest the limit is 2,000 characters.
Not that it helps, but could you add code to the 'before update' to display what the new size should be wnen concatenated? May help to figure a solution...
Be sure to save the form before making any changes with:
If Me.Dirty Then Me.Dirty = False
Workaround 1: Try to update not a field of an underlaying query or table, but a field of the form's Recordset. For example:
With Me.Recordset
.Edit
!MemoField = "TEXT ADDED HERE <- " & !MemoField
.Update
End With
Workaround 2: If you need to update not form's Recordset, but any other Recordset, try to unbind the TexBox, make the update and then make the TextBox binded back again.
I had the same issue and I closed all other open tabs (tables, etc...) and tried again. That worked fine.
I had this problem years ago & was revisiting in case it as not an issue anymore, but seems it is.
I had a list/column form of emails inc a field with a large rft memo.
I wanted to click on a record to bring up the one email in single form
The only way I could get it to work was Open the Single form & immediately close the multi row form, on exiting the single had to load the list again using bookmarks etc. to return to where I wanted to be, a real pain.
Especially as like now I would like to be able to access the the list whilst still on the single form, hence the revisit, instead looks like I will have to save all the list'id somehow. Dooable but a lot more programming that I think should be necessary.

Access 2007: Filtering a report's results using a drop-down box

My question is twofold.
I have around twenty assorted tables in a database. The table layouts are diverse; the one common thread is that all of them have a 'County' field.
I need to set up a series of reports which allow a user to select a county from a drop-down box, triggering the report to run and return only records attached to that particular county.
This is doable at the datasheet level using a filter-by-form, but that's pretty clunky and I have several tables/queries which will need this same county filter.
I may be halfway there with the following:
Create an unbound form.
Add a combo box.
Set the Row Source of the combo box to include the County field.
Set its Bound column to 1.
Set its Column Count property to 2.
Set the Column Width property to 0";1"
Name the Combo Box 'ChooseCounty'.
Add a Command Button to the form.
Code the button's click event as follows:
(Note: To write the code, in Form Design View select the command button. Display the button's property sheet.
Click on the Event tab.
On the On Click line, write:
[Event Procedure]
Click on the little button with the 3 dots that appears on that line.
When the code window opens, the cursor will be flashing between two already existing lines of code.
Between those lines, write the following code.)
Me.Visible = False
Close the Code window.
Name this form 'ChooseCounty'.
In the Query that is the Report's Record Source [County] field
criteria line, write:
forms!ChooseCounty!ChooseCounty
Next, code the Report's Open event:
(Using the same method as described above)
DoCmd.OpenForm "ChooseCounty", , , , , acDialog
Code the report's Close event:
DoCmd.Close acForm, "ChooseCounty"
When ready to run the report, open the report.
The form will open and wait for the selection of the Company.
Click the command button and then report will run.
When the report closes, it will close the form.
I can persuade the report to trigger the form, but only once - I can't seem to figure out where precisely the 'forms!ChooseCounty!ChooseCounty' needs to go. Perhaps someone can clarify or offer a more elegant way to do this?
I need to set up a large meta-report containing sub-reports on all of the tables - and, using the same drop-down 'choose a county' form, I need to have that choice cascade down through all the subreports. I don't have the faintest idea how to go about this. Suggestions welcome!
~ T
You seem to be asking two questions, the last of which is clear to me, but the first is not. The second one is in regard to how to cascade the filter to the subforms. You can do this in one of two ways:
put the form control reference as criterion in the recordsource of each subreport, OR
create a non-visible control on the report that has as it's controlsource "=Forms!ChooseCounty!ChooseCounty". Name that control "CountyFilter". Then, add CountyFilter to the link properties. If, for instance, you are linking the subreports on ID, you'd have:
LinkMaster: ID;CountyFilter
LinkChild: ID;County
(assuming, of course, that ID is your link field for the child reports, and that "County" is the name of the field in the child subreport).
Now, I'm wondering why you would have the County data not just in the parent record but in the child records -- that makes no sense. If you do have it, then the solution above will work.
If you don't, then I don't understand the question, as the whole idea behind subreports is that they are filtered by the parent record, so if the parent record is a person, and you filter by COUNTY, you're only going to get the child records in the subreport for that person, which by definition are already filtered by COUNTY because the parent has been filtered.
As to the earlier question, you write:
I can persuade the report to trigger
the form, but only once - I can't seem
to figure out where precisely the
'forms!ChooseCounty!ChooseCounty'
needs to go
You have two choices:
hardwire the recordsource of the report to use the form control reference, so the WHERE clause of your report would be "WHERE County=Forms!ChooseCounty!ChooseCounty" (and you should set this as a parameter of type text to insure that it gets processed correctly).
the better meethod is to set the recordsource in the report's OnOpen event.
After you open the form as a dialog, you'd have something like this:
Me.Recordsource = "SELECT * FROM MyTable WHERE County='" _
& Forms!ChooseCounty!ChooseCounty & "'"
And immediately after that line, you can close the form, since it's not needed any longer.
You will likely want an OnNoData event for the case where no records are returned. This is usually something simple like:
MsgBox "No records found!"
DoCmd.Close acReport, Me.Name
I hope this answers your questions, but if not, I'm happy to offer more explanation.