How to update another text box while typing in access 2007 form? - forms

I have a few text boxes which have to be filled with numeric values from 0 to 100. Below them there is another text box which stands for a total (the sum of the values from the text boxes above). How can I update the sum text box while typing in any of the other text boxes above?

If you are happy that the sum box updates after a box is updated (enter, tab or such like is pressed), then this can be done without any code. First, you will need to set the format of the textboxes to be summed to numeric, then the control source of the sum box becomes:
=Nz([text0],0)+Nz([text2],0)+Nz([text4],0)+Nz([text6],0)+Nz([text8],0) ...
Note the use of Nz, it may be possible to eliminate this by setting the default value property of the various textboxes to be summed.
A large set of controls that need to be summed in this way is often an indication of an error in the design of the database. You would normally expect this kind of thing to be a separate recordset, which could more easily be summed.

I know this is old, but Google didn't come up with much for this topic and this thread didn't really help either. I was able to figure out a very easy way to do this, so hopefully anyone else looking for this will find this helpful.
My need was for actual text as opposed to numbers, but the same applies.
To do what the OP is asking for you'll need at least 3 textboxes. 1 is the textbox you want to have updated each time you type, 2 is the textbox you will be typing in, and 3 is a hidden textbox.
Set textbox 1 to reference the value of the hidden textbox 3 in its control source:
="something in my textbox " & [textbox3]
In the OnChange event of textbox 2 right a line that will set the value of the hidden textbox 3 to the Text property of textbox 2 that you are typing in:
Private Sub textbox2_Change()
Me.textbox3.Value = Me.textbox2.Text
End Sub
Each time the value of the hidden textbox 3 gets updated, the calculation/reference in the displayed textbox 1 will be updated. No need to save caret locations or anything else mentioned in this post.

I was able to do this in Access 2007 by using the On Lost Focus event of the text box.
Just put something like this on the On Lost focus event of each text box that you want to be added , just make sure to set the default value of each text box to 0.
Me.Totals.Value = Me.Text1.Value + Me.Text2.Value + etc..
The moment you click on the next text box or anywhere as long as it loses focus, your sum will already be on the Totals box. You may add as many text boxes as you like, just include them in the code.

This is problematic due to the asinine requirement in Access that you have to set focus to text areas before you can get their value. I would recommend you change your design so that the text field is updated in response to a button click instead of on change.
If you want to go the update-on-change route, you would attach change events to each of the addend text fields. The event handlers would need to save the caret position/selection length, update the sum in the output text field, and restore the caret position. You need to save/restore the caret position because this is lost when the focus changes.
Here's an example for two text fields (txt1 and txt2). The output field is named txtOutput.
Private Sub txt1_Change()
Dim caret_position As Variant
caret_position = Array(txt1.SelStart, txt1.SelLength)
UpdateSum
txt1.SetFocus
txt1.SelStart = caret_position(0)
txt1.SelLength = caret_position(1)
End Sub
Private Sub txt2_Change()
Dim caret_position As Variant
caret_position = Array(txt2.SelStart, txt2.SelLength)
UpdateSum
txt2.SetFocus
txt2.SelStart = caret_position(0)
txt2.SelLength = caret_position(1)
End Sub
Private Sub UpdateSum()
Dim sum As Variant
sum = CDec(0)
txt1.SetFocus
If IsNumeric(txt1.Text) Then
sum = sum + CDec(txt1.Text)
End If
txt2.SetFocus
If IsNumeric(txt2.Text) Then
sum = sum + CDec(txt2.Text)
End If
txtOutput.SetFocus
txtOutput.Text = sum
End Sub

Related

LibreOffice macro showing simple TextBox shape

I cannot figure out (or find an example) how to perform the following simple thing in the LibreOffice Calc 6.2:
I have a drawing shape (e.g. a simple rectangle) in a sheet (call it ShapeA) and a textbox shape in another sheet (call it TextboxB). I want to do the following: when I click on the ShapeA, the TextboxB must appear on the screen (without changing the current sheet, maybe in a dialog box) and then be closed with a mouse click.
I guess the macro associated with ShapeA could look something like this:
Sub Main
oDrawPage = ThisComponent.getDrawPage()
oTb = oDrawPage.getByName("TextBoxB")
oTb.show()
End Sub
Could someone advise what I should put into this macro to accomplish the described task?
UPDATE: What I want to accomplish ( in reply to Jim K.).
I have a very cluttered diagram with many shapes. Each shape has some textual information associated with it. There is not enough space on each shape or around it to contain this info. So there is must be a way to display this info about each shape. Also this information should be displayed in a preformatted way (it contains code and other structured info).
My plan is to create a textbox with the relevant information for each diagram shape, place these textboxes in other sheet and have a possibility, when viewing diagram, to click on any shape and view the associated info in the poped up textbox without leaving the diagram, and then close the textbox with a simple action (e.g. by clicking on it).
Does this task sound feasible to be realized with the LO's shapes and macros?
How about this: Put everything on the same sheet but keep the text boxes hidden until needed.
Use the following code adapted from https://ask.libreoffice.org/en/question/93050/how-can-i-hideshow-a-shape-of-my-spreadsheet-using-a-macro/.
Sub ShapeClickedA
ShowHideShape("TextBoxA")
End Sub
Sub ShapeClickedB
ShowHideShape("TextBoxB")
End Sub
Sub ShowHideShape(shapeName As String)
oDrawPage = ThisComponent.getSheets().getByName("Sheet1").getDrawPage()
For iShape = 0 To oDrawPage.Count - 1
oShape = oDrawPage.getByIndex(iShape)
If oShape.Name = shapeName Then
If oShape.Visible Then
oShape.Visible = 0 'Not Visible
Else
oShape.Visible = 1 'Visible
End If
End If
Next iShape
End Sub
If you haven't yet, set the names of the text boxes by right-clicking and choosing Name... Then right click on both ShapeA and TextBoxA and assign the macro ShapeClickedA. Do likewise for other pairs of shapes. The result works like this:
Before anything is clicked.
Click on ShapeA. (To close it again, click on either ShapeA or TextBoxA). ShapeB functions similarly.
It's also possible to display both at the same time.

MSAccess: Call a summation from a subform (in datasheet view) to be displayed in a textbox on the master form?

Sorry if this question is simple but I have googled and haven't found a satisfactory answer.
I'm creating an engineering cost estimator. I have a form that takes inputs as ISO/Drawing, and for each drawing number are many subforms where you can input ComponentDesc and should spit back out a TotalHours to complete number. I've included a picture (sorry for bad quality)
From the image, the table on the bottom is a subform in datasheet mode (which is usually hidden and located in the footer) which will calculate appropriate Total MH (manhours) for the ComponentDesc inputted into the subform on the right. I would like the small (and incomplete) textboxes to the left (below the title "MH Totals for ISO/Drawings") to display the aggregate total from the subform on the bottom.
I've been trying to use DSUM() to define Control Source for the textbox but it keeps coming back with #ERROR as seen in the textbox to the left. Right now what I have typed out is:
=DSum("[Total MH]","frm-PipingHandleMH")
in the expression builder. [Total MH] being my field and frm-PipingHandleMH being the subform on the bottom. I've tried to put brackets around everything but it didn't work (even though I'm not exactly sure what brackets usually do). Any advice?
DSum (and all domain aggregate functions) acts on a table or query. If you want to use that approach you need to refer to the source of that form and use a filter parameter to limit to appropriate records the I.E. if the form's datasource is qry-PipingHandles and if the form you are trying to sum on currently is showing handles for widget 4 then it would be something like:
=dsum("[Total MH]","qry-PipingHandles","[widgetID] = 4")
Note that if that 4 was the currrent state of form then you need to pass it in, so something like:
=dsum("[Total MH]","qry-PipingHandles","[widgetID] = " & [frm-PipingHandlesMH]![WidgetID].Value)
Where you reference the field in the form and append it onto the string that is applied as a filter to the source for Dsum.
Another approach is to put a subtotal in the footer of the form (iirc you don't actually need to show the footer) and then reference that footer control from the parent form.
Brackets are needed to demarcate names that include spaces or other odd characters, they also can be used (e.g. in query design view) to force Access to treat something as a name rather than a string literal.

Excel Form VBA Combobox reset

This should be easy but I can't seem to find anything to solve. I have a Form in Excel using VBA. The Excel sheet has four columns populated with data and the Form displays those fields.
The user then selects a value in a combobox, uses a command button to submit the responses and the values from the combobox are written to the Excel sheet.
The user then uses a command button to advance one row down in the spreadsheet and load the four values from a new row. This all works great.
The issue I am trying to solve is that the combobox remains selected to the value in the prior selection. I'd like to reset the combobox so nothing is selected and the user has to make a selection again for the next row.
Below is the code I am using to load the combobox and to set a variable for what the user selected. Can't seem to get the combobox back to it's default state after the user has submitted the form.
Private Sub cbDesAccWanted_Change()
Select Case cbDesAccWanted.Text
Case "Yes"
desacc = "Yes"
Case "No"
desacc = "No"
End Select
cbDesAccWanted.Text = desacc
End Sub
Private Sub cbDesAccWanted_DropButtonClick()
cbDesAccWanted.List = Array("Yes", "No")
End Sub
There are two ways to reset the combobox. Take your pick:
1
cbDesAccWanted.Value = Null
2
cbDesAccWanted.ListIndex = -1
the line
cbDesAccWanted.Text = desacc
is totally unnecessary.
Using cbDesAccWanted_DropButtonClick is not the right place to populate the list of values. This list should be set up when the form is first shown to the user.
(unelss the values it shows chnages in which case change it when the row changes or something, not when the user clicks on it)
So when theuser clicks the down arrow to move to thenext record include the following line
Me.cbDesAccWanted.text = Me.cbDesAccWanted.List(1)
Note (1) access teh 2nd item in the list which is No.
So this reset it to the default value of No.
Ok.

How do I set size of colums in a table subform datasheet view in MS Access

I have a subform bugging me. The mainform contains buttons etc. Everytime the user close/open the form, the columns width is reset to whatever the table likes. If i open the table directly, the size is as i want. If i change it in the subform, it is not saved. (See screendump)
I would like "Phase" to stay about 2 cm width. I can enter the subform and edit the "Width" but that is only applied to the other views.
I hope you can help, Emil.
I realize this post is almost 2 years old, but I ran into the same problem and came across this post.
I am running MS Access 2013 on Windows 7 Ultimate. I did not find the solutions offered here to work for me, so, I set out to find something that would work for me before I went to VBA code. (Incidentally, I appreciate the link offered by #Patrick_Honorez in his comment on the original post because that was going to be my fall-back solution.)
Anyway, here is what worked for me and I hope perhaps it will work for others as well:
Open the parent form.
In the subform, right-click the column header
for the column for which you wish to adjust the width.
Select the “Field Width” item from the context menu.
In the “Column Width” dialog that appears in step 3, enter the desired column width in points, or, use the [Best Fit] button. Then click the [OK] button to close the dialog and return to the form.
Right-click the parent form’s border to bring up the parent form’s context menu. Click the “Save” item in the context menu.
Now close the parent form.
The next time the form is loaded, the column widths should be as set it step 4 above--at least they are for my setup.
I see this post is quite old and OP must have figured someway to tackle the issue. I came across same issue today and found solution on this link.
For anybody else having same issue, use following code (I modified the code a little because original code from the above mentioned post saves column width of only text boxes but my form has combo boxes too, column width of which was not getting saved) in close and open events of your subform and then open main form in Form View and then manually select desired widths either by mouse, by entering column width value or using best fit. Save the form and reopen to check results. That's it.
Private Sub Form_Close()
Dim ctrl As Control
For Each ctrl In Me.Controls
If (ctrl.ControlType = acTextBox Or ctrl.ControlType = acComboBox) Then
SaveSetting "propertiesDBS", Me.Name, ctrl.Name, ctrl.ColumnWidth
End If
Next
End Sub
Private Sub Form_Open(Cancel As Integer)
Dim ctrl As Control
Dim frm As Form
Dim w As Long
For Each ctrl In Me.Controls
If (ctrl.ControlType = acTextBox Or ctrl.ControlType = acComboBox) Then
w = GetSetting("propertiesDBS", Me.Name, ctrl.Name, 0)
If w <> 0 Then ctrl.ColumnWidth = w
End If
Next
End Sub
I know this is late to the party and most likely going to be the last comment anyone reads, but this can be done quite simply in MS Access 2016 - by someone like myself who has no more than 4 days experience in databasing overall and no experience with writing custom Macro's or VB Script (using only what is native to MS Access).
Here's how I did it.
Scenario - Split Form (Form + Datasheet).
Extra Recommendations:
It pays to be across all properties of every object type in your database, as a change in a field property can cause unpredictable erratic effects, which take ages to figure out why it happened and how to stop it from happening again, whilst delivering your desired outcome.
Me.Requery in your VBA script after every necessary event and also in your main form (generally the 'After Update' event is used most), and be wary that too many Me.Requery's (in unnecessary events) can also be detrimental - so too much of a good thing can be a bad thing.
Bottom Line Up Front - Modify the format of your query that is to be exported/printed.
In 'Design View' of the query you are concerned with, ensure that the fields are in the order you need them outputted in first as this is exactly how the macro will present them for export/print (example could be "Australia" then "Northern Territory" then "Town's In The Northern Half Of The State" then "Darwin" then "Suburbs In The Northern Half Of City").
Switch to 'DataSheet View' in the same query and use the top left folded over triangle looking thingy to highlight the entire data set then right click that same triangle to present an options menu. Select 'Row Height' and type in "15" (default row height in Excel).
Deselect the entire spreadsheet and this time select every column (left click the left most column, hold shift button, scroll over to the right to the far end of the data set and click the last column) and then right click one of the highlighted columns to present another menu. Select 'Field Width' and within that new pop-up menu select 'Best Fit' and then 'OK'.
(Optional - not sure if this helps or hinders but I did it for my purpose) With the columns still selected right click one of the highlighted columns again and select 'Freeze Fields'.
My scenario had buttons with macros configured to run filtered reports so I was able to check this by simply clicking any of those buttons and seeing the report formatting, which it held true to the work I had just done. I exported using another button with a macro that exports to Excel with 'Print Formatting' selected (my main form also had the datasheet view as the only thing that could be printed and was also set in 'Print' formatting.
The Excel spreadsheet opened with all row heights and column widths in a way that I could read every field/record with perfect ease and without extra modification.
This also worked for cascaded combo boxes, with the export only outputting the 'drilled down/filtered' datasheet records, in a format that required no further modifications.
Hope this helps someone, as its saved my hide! :)
Open the Main form in Design. Go to the SubForm. Click on the square at the top left of the SubForm and select 'Properties'. Right-Click the control 'Phase' and click 'Properties'.Click the 'Format' tab and select 'Width'. What do you see there? That should control the widht of control 'Phase' unless you have some overriding coding elsewhere. Input the size you want and see what happens.
Use continuous forms instead. It gives you complete control over how your subform displays.
If you open your subform directly, your property sheet menu should display automatically if the default view is "Datasheet." Click on "All" and change the "Auto Resize" property to "No." This should solve the issue and avoid the need for VBA.
This only works when you open the subform separately. So if you want the changes to be reflected within your main form, you'll have to close it and switch back and forth.
Super annoying by default.
It seems to work as one would expect of you set the view mode to layout view. Drag column widths as needed and save. Go back to form view and it works. It's really dumb it doesn't work the same way in form view our design view.
In Access 365, open main form, right-click sub-form datasheet columns that need width adjustment, use the Field Width to adjust, click on border of main form to select Layout view, and save changes.
Open subform in datasheet view (by double click on subform in the left pannel)
Resize columns as you want by dragging or by right-click the column header for the column for which you wish to adjust the width and select the “Field Width” item from the context menu.
Right-click the subform border to bring up the context menu. Click the “Save” item in the context menu.
Either open the Main Form in Layout View or directly open your Subform in Datasheet View. Right Click on the Field Header, select Field Width, and enter the desired width. Save. Bewm.
My solution (Access 2016) was to create the main & subform, recreate the subform on its own using form wizard and set it up the way I want it, rename the original subform to something else, and finally rename the recreated subform to the original form name. Open the main form and the subform should be laid out the way you want it. You can then delete the original subform you renamed.

Access Form with checkbox toggled textboxes to make visible

Im trying to cut down on the clutter of my form since the data im getting can fill in more or less fields from my table.
as of right now im trying to build an event but I do not know the right syntax to use to create my event.
right now i have:
= if toggle.onclick ="yes" then
data.visible=true
else
data.visible=false
end if
in the After Update tab of the Event tab.
I hope that gives you an idea of what im trying to do.
I have this on a test form so the only objects are:
checkbox name "toggle"
textbox name "data"
the text box is default to not visible at the moment.
my goal is to have a list of check boxes and once they are checked their corresponding text box would appear on a refresh. this way the workers wont be intimidated by the amount of textboxes are on my current form. also will reduce the vast clutter on the current form.
By default, the 'Toggle' value will be True or False - not 'yes' or 'no'. Thus the following is what you need to toggle fields:
Private Sub Toggle_AfterUpdate()
If Me.Toggle = True Then
Me.Data.Visible = True
Else
Me.Data.Visible = False
End If
End Sub