Access 2013 - Save Value from Textbox and display again when form is opened after close - vba

I want to save a value from textbox in a string for example and display it again when the form get's openend.
I have two textboxes PriceRangeOne and PriceRangeTwo .. The user enter here for example 20 and 40
The problem i have is that when the user switches between Form and Report the values in this textboxes are beeing deleted. How can i save them?
I tried adding a sourcecontrol to the fields but had name errors eventhough i used different names.
I tried adding this to on change and retrieve it in an onload
Dim eingabe As String = textBox1.Text or .Value
Still didn't worked. Does anyone know a way to do this?

Typically, the most efficient and reliable way to do this is to have some form auto-open when the database is opened. It could be a dashboard, or just some form with nothing else on it. Whatever you use, launch it when the database opens and then minimize it. Now you have a form that's always open, as long as the application is open. Add a couple of textboxes to this form/dashboard.
When you close your form referenced in this question, write the values of PriceRangeOne and PriceRangeTwo to the textboxes on the form I described above. Then, when you open a new form or report, you can reference the values in those textboxes. Since the form is always open, you can reference these values at any time from any form or report until you close your database.

Solved it with variables.
I declared global variables in my standart module
For example
Public PriceOne As Double
Public PriceTwo As Double
Than i did this in my form in Close() and Open():
Private Sub Form_Close()
PriceOne = Me.Field
PriceTwo = Me.FieldTwo
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.Field = PriceOne
Me.FieldTwo = PriceTwo
End Sub
Works perfect!

Courtesy of How to save the last value of a textbox. | Access World Forums:
Private Sub Form_Close()
'Set the default value for textbox
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE table SET table.field = [Forms]![FormName]![Textbox] " & vbCrLf & _
"WHERE (((table.ID)=1));"
DoCmd.SetWarnings True
End Sub
Private Sub Form_Load()
'Load the default value for textbox
Me.Textbox.Value = DLookup("[field]", "[table]", "[ID]=1")
End Sub

Related

int array to returning to previous record

I have a standard form which displays information based on a partID. The form has a subform showing ancillaries of the part where on double click will take you to that part number (code snippet below). var_lastPartID is a global long variable which records the current ID so upon pressing a return button will take you back to the previous record. However, as this can only store 1 value at a time I imagine the best way would to be to store an array of long/int whereby upon the double click the current ID is stored. When you click return it will take the last value added and take you to that record then delete that record, so that the next time you click return it will take you to the next record. However, my experience with VBA is very limited and I have not used them before. Please could someone explain the syntax of how I could achieve this?
Private Sub childPart_DblClick(Cancel As Integer)
var_lastPartID = Forms![Part]![part_ID].Value
Forms("Part").Recordset.FindFirst ("part_ID = " & childPart)
End Sub
Ok, so you have a parent form, and then a sub form.
You click on a row in the sub form. You want the parent form to JUMP to this record. But as you may well do this several times, then you want on the main form some kind of “back” or “previous” button. When you click on this back button, then you want the previous ID that you jumped to occur.
Ok, the way to drive this and have a next/previous set of buttons that traverse the records that you looked at, worked on, and clicked on?
Ok, in the main form, at the code module level, create a pointer, and a collection, and a routine like this:
Option Compare Database
Option Explicit
Dim intListPosition As Integer
Dim colRecords As New Collection
Public Sub AddAndJump(MyID As Long)
' add this new ID to the list
intListPosition = intListPosition + 1
colRecords.Add MyID, CStr(intListPosition)
' now jump (move) to this record
Me.Recordset.FindFirst "ID = " & MyID
End Sub
Public Sub BackOne()
' call this code from your back buttion
' go to previous record
If intListPosition > 1 Then
intListPosition = intListPosition - 1
Me.Recordset.FindFirst "ID = " & colRecords.Item(CStr(intListPosition))
End If
End Sub
Public Sub ForwardOne()
'call this code from your forward buttion
If intListPosition < colRecords.Count Then
intListPosition = intListPosition + 1
Me.Recordset.FindFirst "ID = " & colRecords.Item(CStr(intListPosition))
End If
End Sub
Private Sub cmdBack_Click()
BackOne
End Sub
Private Sub cmdNext_Click()
ForwardOne
End Sub
So the above goes in the main form.
In my "sample" code, I used ID, you have to change that to part_id
Now, you button code in the sub form is going to be "similar" to what you have, but you can now use this:
Call Me.Parent.AddAndJump(Me.ID)
So, the above will now allow a next/back button on the top form, as you click away and select the items from the lower form (the sub form), then the list of "id" will be built up, and the buttons for next/prev will work.

Microsoft Access applying 1 function to all fields automatically

I have a form that keeps track of assigned patient equipment. I have it set so that any changes made to text fields on the form automatically move down to the "comments" section of the form (this is done so that any changes made are documented in case the user forgets to manually document changes). I have a sub that I wrote that accomplishes this that I am currently calling for every single text field. This works but is messy.
Is there a way to apply the sub to all the fields in one procedure without calling it for every individual field? Code is below, please let me know if I can clarify anything.
Private Sub pPEMoveValue(sField)
'Moves the old field value down to the comments section automatically
Dim sOrigValue As String
Dim sCommentValue As String
sOrigValue = sField
sCommentValue = Nz(mPEComments, "")
Me.mPEComments = sCommentValue & vbNewLine & sOrigValue
End Sub
Private Sub sPEBatCharger_Dirty(Cancel As Integer)
pPEMoveValue (Nz(Me.sPEBatCharger.OldValue, ""))
End Sub
This is the solution I came up with to do what you are looking to do. I took advantage of the MS Access Tag system. You can add tags to your controls so you can sort of "Group" them.
First put the form in design view and adjust the tag for all of the fields you want to record to say "Notes".
Then in the Form's BeforeUpdate even you would add this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Call FindControlsForComments(Me.Form)
End Sub
Then you would use this function to find any fields that have the "Notes" tag and run it through the function you created:
Public Function FindControlsForComments(frm As Form)
Dim ctrl As Access.Control
For Each ctrl In frm
'If the control is tagged for notes
If ctrl.Tag = "Notes" Then
'If the old value is different than the current value
If Nz(ctrl.OldValue, "") <> Nz(ctrl.Value, "") Then
'Add to comment
Call pPEMoveValue(Nz(ctrl.Value, ""))
End If
End If
Next ctrl
End Function
You may have to adjusted this slightly to work with your system but this has worked well for me.

ms access vba forms, textboxes, queries

I am fairly new here.
I am using access 2013 with vba only, no macros.
One table called tblStoreCode with three fields, one being the autonumber, the others are fldStoreCode and fldStoreName.
My form called frmStoreDetails has two textboxes, txtStoreCode and txtStoreName and a cmdSearch button.
What I would like is when a user enters a code into txtStoreCode and clicks the button then the sql statement must look at the value in txtStoreCode and then look in tblStoreCode, find the Code and then display the Store Name in txtStoreName on the form.
I tried so far
Private Sub Command9_Click()
Dim MyString As String
MyString = CurrentDb.QueryDefs("qryStore").OpenRecordset.Fields("fldStoreName")
Me.txtName.SetFocus txtName.Text = MyString End Sub
Please help.
This how i was doing it-
Private Sub cmdSearch_Click()
txtName = nz(Dlookup("fldStoreName","tblStoreCode","aa=" & nz(txtStoreCode,0)),"")
End Sub
Or like this if the nz function Complicates you (nz function will help prevent problems when there is null value)
Private Sub cmdSearch_Click()
txtName = Dlookup("fldStoreName","tblStoreCode","aa=" & txtStoreCode,0)
End Sub
Have fun!

Update value in another form's textbox via VBA

Hopefully this makes sense. I'm frustrated that I cannot figure this out. I have a simple Access 2010 database. I have a simple form inside it that helps the user input some specific information. This data entry situation can occur on two other forms in the database. Rather than have two copies of the "helper" form where the VBA code has hard-coded control references, I wanted to make it more universal by passing the name of the form that calls it by using the openArgs parameter.
When the time comes to transfer the values BACK to the form that needs the information, the helper form attempts to do this like so:
Private Sub cmdOk_Click()
Dim theFormName As String
Dim theForm As Form
theFormName = Me.OpenArgs
Set theForm = Forms.Item(theFormName)
If Not IsNull(theForm.Name) Then
theForm.txtLongitude.Value = Me.lblLongitude.Caption
theForm.txtLatitude.Value = Me.lblLatitude.Caption
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
The variable theForm is populated correctly and theForm.Name returns the correct name of the form so that part works fine. The problem is that theForm.<controlName>.Value does not. When I run the code, I get an
application-defined or object-defined error (Run-time Error 2465)
I've tried all sorts of permutations for the control reference from the current open form to a second open form but I cannot get the syntax right. I've tried:
theForm!txtLongitude.Value ("..can't find the field txtLongitude..")
theForm.Controls("txtLongitude").Value ("..cant find the field...")
I have two suggestions. If one works, let me know and I'll edit my answer to only include the one that works.
Try changing theForm.txtLongitude.Value = Me.lblLongitude.Caption
to Forms!theForm!txtLongitude.Value = Me!lblLongitutde.Caption and theForm.txtLatitude.Value = Me.lblLatitude.Caption to Forms!theForm!txtLatitude.Value = Me!lblLatitude.Caption
If you've already tried that or it doesn't work, try declaring variables and "pulling" the values out of one form before putting them in the other form. (Also make sure the data type of both are the same.)
Private Sub Command4_Click()
Dim theFormName As String
Dim theForm As Form
Dim txtLong As String
Dim txtLat As String
txtLong = Me.lblLongitude.Caption
txtLat = Me.lblLatitude.Caption
theFormName = Me.OpenArgs
Set theForm = Forms.Item(theFormName)
If Not IsNull(theForm.Name) Then
theForm.txtLongitude.Value = txtLong
theForm.txtLatitude.Value = txtLat
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub

Problems when calling a public sub

I'm facing a deadend When trying to call this sub :
Public Sub backblue(ByVal frm As Form, ByVal boxname As String)
For i = 1 To 3
CType(frm.Controls(boxname & i.ToString()), TextBox).BackColor = Color.LightBlue
Next
End Sub
with button click event :
Private Sub Button1_click and bla bla....
backblue(Me, "txb1_")
End Sub
Can anybody show me a suggestion to fix the code.
It throws "Object Referrence not set to an instance bla bla" error
For information the textbox names are :
txb1_1 , txb1_2 , txb1_3
(these are some of the many textboxes in the form that i want its bakcolor changed)
and these three textboxes are already created through designer, not from execution.
i did check the textboxes names and there's nothing wrong.
the form class is also public.
if they are the only textboxs on said form you can just loop through
For Each box as Textbox In frm.Controls
box.BackColor = Color.LightBlue
Next
This error will occur if you do not declare the Form class to be public.
Also, make sure the textbox names are really correct, although this will probably cause a different error.
If you create the textboxes during execution, make sure they are initialized with New and added to the form's Controls collection.
Try this....
Public Sub backblue(ByVal frm As Form, ByVal prefix As String)
For i = 1 To 3
Dim bxName as String = prefix & i.ToString()
Dim bx as TextBox = CType(frm.Controls(bxName), TextBox)
If bx Is Nothing Then
MsgBox("Unable to find text box " +bxName)
Dim mtch() As Control = frm.Controls.Find(bxName, true)
If mtch.Length> 0 then
bx = mtch(0)
Else
Continue For
End if
End If
Bx.BackColor = Color.LightBlue
Next
End Sub
Although, a better solution would be to either create the textboxes inside a control and pass that control to BackBlue or to create an collection that has the controls and pass that in. Which brings up what is most likely yor problem your control is contained in a sub component and thus is not in the main form control collection
Alternative, you could use either the tag of the control or create a component control that implements IExtenderProvider and add it to the form --all of the above would effectively allow you to define the controls and/how they should be handled at designtime.
It may really seem that the names generated by this loop may not be the names of the original textboxes. My suggestion is before setting this Color property verify that the names generated by this loop are indeed the actual names. Maybe output this in a messagebox:
MessageBox.Show(boxname & i.ToString()) for each loop before you set the property