ms access vba forms, textboxes, queries - sql

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!

Related

How to get a form field to auto fill when a product is entered?

I am trying to get my form to populate the days needed for testing automatically when I enter a new record but it keeps erring out. I am very new to using VBA and Access 2016.
I have looked at some other examples that people have posted that work and cannot get it to work.
I am continually getting debugger.
Option Compare Database
Private Sub Fill_SKU_AfterUpdate()
PopulateFields
End Sub
Private Sub PopulateFields()
frmSerialTracerLog.Days_Used_For_Off_Test = DLookup("Days_Used_For_Off_Test", "tblTestDays", "Fill_SKU = '" & frmSerialTracerLog.Fill_SKU & "'")
End Sub
You probably are referring to the current form, thus use Me:
Private Sub PopulateFields()
Me!Days_Used_For_Off_Test.Value = DLookup("Days_Used_For_Off_Test", "tblTestDays", "Fill_SKU = " & Me!Fill_SKU.Value & "")
End Sub

Sort List-box Conditionally without changing the Row-source

If I got a list box say lstABC with 3 columns A, b & c.
And there is 3 sorting buttons say btnSortA, btnSortB, btnSortC.
Is there a way to use the on_click event on the buttons that allow the user to sort lstABC without changing lstABC.rowsource every time?
I was trying to achieve something in the line of this:
Private sub btnSortA_Click()
lstABC.Orderby = "ColumnA ASC"
me.lstABC.OrderByOn = True
End Sub
Sure I could build SQL strings and set/requery lstABC.rowsource = string(for every button), but I am looking for something simple and efficient that don't overcomplicated things. Thanks
In my opinion, you can not make it simplier than changing the row source. You could also read all rows in the listbox and reinsert all the rows in the right order with .addItem...
Private Sub sortListbox(criteria As String)
Dim strSQL As String
strSQL = "SELECT A,B,C FROM TableName" _
& " ORDER BY " & criteria
Me.yourListboxController.RowSource = strSQL
Me.yourListboxController.Requery
End Sub
And you just add in the click event :
Private Sub btnSortA_Click()
Call sortListbox("A")
End Sub
Private Sub btnSortB_Click()
Call sortListbox("B")
End Sub
Private Sub btnSortC_Click()
Call sortListbox("C")
End Sub
I do not think you can have simplier than that.

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

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

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.

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