I am new to VBA code, and working on a project to revised a built MS Access database that has some VBA code built from another person. Can some one explain what the code below would do? My end goal to to create a Data Entry form that would keep previous value from some fields when the users enter the next New Record. Thank you so much.
'Private Sub DocumentTypeCombo1_AfterUpdate()
'Me.DocumentNameCombo1 = Null
'Me.DocumentNameCombo1.Requery
'Me.OrderBy = "Errors DESC"
'Me.DocumentNameCombo1 = Me.DocumentNameCombo1.ItemData(0)
'End Sub
'Private Sub DocumentNameCombo1_AfterUpdate()
'Me.SubcategoryCombo1 = Null
'Me.SubcategoryCombo1.Requery
'Me.OrderBy = "SubCategory DESC"
'Me.SubcategoryCombo1 = Me.SubcategoryCombo1.ItemData(0)
'Me.DocumentNameCombo1.Tag = Me.DocumentNameCombo1
'End Sub
It is commented out, so it does nothing. Even if not, it wouldn't as it misses this line:
Me.OrderBy = "Errors DESC"
Me.OrderByOn = True
If you include the line, it will rearrange the sorting of Me.
To ...
keep previous value from some fields when the users enter the next New Record
you would set the DefaultValue (always a string value) in the AfterUpdate event:
Me!SomeControl.DefaultValue = Chr(34) & Me!SomeControl.Value & Chr(34)
Related
I'm trying to setup a simple filter button that will filter by the network user ID which is populated in the [Owner] field when a record is created. The idea is to see only the records you created.
the code I'm using is as follows:
Private Sub FilterUser_Click()
Dim Uname As String
Uname = Environ("USERNAME")
Me.Tasks.Form.Filter = [Owner] = Uname '<---- Error debugs this line
Me.Tasks.Form.FilterOn = True
End Sub
I get the following error but I'm confused on what field it thinks is missing.
Any help would be greatly appreciated.
Filter requires a string value.
me.tasks.form.filter = "[owner]='" & uname & "'"
Code to create new form instance of a closed form using form name
I want to replace the long Select Case list with a variable.
Full code of module
In Access 2010 I have a VBA function that opens a new instance of a form when given a string containing the form's name. By adding a form variable "frm" to a collection:
mcolFormInstances.Add Item:=frm, Key:=CStr(frm.Hwnd)
The only way I can figure out to open "frm" is with a Select Case statement that I've manually entered.
Select Case strFormName
Case "frmCustomer"
Set frm = New Form_frmCustomer
Case "frmProduct"
Set frm = New Form_frmProduct
... etc ... !
End Select
I want it to do it automatically, somewhat like this (although this doesn't work):
Set frm = New Eval("Form_" & strFormName)
Or through some code:
For Each obj In CurrentProject.AllForms 'or AllModules, neither work
If obj.Name = strFormName Then
Set FormObject = obj.AccessClassObject 'or something
End If
Next
Set frm = New FormObject
I just want to avoid listing out every single form in my project and having to keep the list updated as new forms are added.
I've also done some testing of my own and some reading online about this. As near as I can tell, it isn't possible to create a new form object and set it to an instance of an existing form using a string that represents the name of that form without using DoCmd.OpenForm.
In other words, unless someone else can prove me wrong, what you are trying to do cannot be done.
I think you are looking for something like this MS-Access 2010 function. (The GetForm sub is just for testing):
Function SelectForm(ByVal FormName As String, ByRef FormExists As Boolean) As Form
For Each f In Application.Forms
If f.Name = FormName Then
Set SelectForm = f
FormExists = True
Exit Function
End If
Next
FormExists = False
End Function
Sub GetForm(ByVal FormName As String)
Dim f As New Form
Dim FormExists As Boolean
Set f = SelectForm(FormName, FormExists)
If FormExists Then
MsgBox ("Form Found: " & f.Caption)
Else
MsgBox ("Form '" & FormName & "' not found.")
End If
End Sub
Here's an ugly hack I found:
DoCmd.SelectObject <acObjectType>, <YourObjectsName>, True
DoCmd.RunCommand acCmdNewObjectForm
The RunCommand step doesn't give you programmatic control of the object, you'll have to Dim a Form variable and Set using Forms.Item(). I usually close the form after DoCmd.RunCommand, then DoCmd.Rename with something useful (my users don't like Form1, Form2, etc.).
Hope that helps.
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
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.
I am using a datagridview to display table data and changing values of a particular cell. Depending on requirement I may need to change such values for more than one row.
I am trying to use datagridview1.CellValueChanged to populate a Dataset (i.e. create a collection of changes made) and subsequently saving the changes by clicking on a command button.
My problem is that though for each change, the sub is being called ONLY the last change is being saved. I was thinking of using the Dataset to store multiple records where the values are changed and then SAVE all the rows in the Dataset in the database table (using Update).
Could there be some solution to my predicament.
PS. Before trying this (ADO.net dataset) I was updating a temporary table and then using that I was updating the database.
Grateful for a solution please.
Code:::
Private Sub dGridVwCreaCode_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dGridVwCreaCode.CellValueChanged
Dim qryStr_CodeShtText_Changed As String
Dim var_CodeID_Changed As Long
var_CodeID_Changed = dGridVwCreaCode(e.ColumnIndex - 2, e.RowIndex).Value
qryStr_CodeShtText_Changed = "SELECT Code_ID, Code, Code_Descrip FROM Code_SAP " & _
"WHERE (Code_SAP.Code_ID = " & var_CodeID_Changed & ")"
var_CodeShtText_Changed = dGridVwCreaCode(e.ColumnIndex, e.RowIndex).Value.ToString
If Not CatGenieConnPublic.State = ConnectionState.Open Then
CatGenieConnPublic.Open()
End If
da_CodeShtText_Changed = New OleDb.OleDbDataAdapter(qryStr_CodeShtText_Changed, CatGenieConnPublic)
da_CodeShtText_Changed.Fill(ds_CodeShtText_Changed, "Code_SAP")
cb_CodeShtText_changed = New OleDb.OleDbCommandBuilder(da_CodeShtText_Changed)
ds_CodeShtText_Changed.Tables("Code_SAP").Rows(1).Item("Code_Descrip") = var_CodeShtText_Changed
To save the changes (following sub being called from a Button_Click):
Private Sub Save_Changed_CodeShtText()
da_CodeShtText_Changed.Update(ds_CodeShtText_Changed, "Code_SAP")
MsgBox("Changes saved to database....", vbOKOnly + vbInformation)
If CatGenieConnPublic.State = ConnectionState.Open Then
CatGenieConnPublic.Close()
End If
'SET BOOLEAN TO FALSE AS CHANGED VALUES HAVE BEEN SAVED
bool_CellVal_HasChanged = False
End Sub
PS. Somehow I am not able to place all the code lines together, pl pardon me.
What I was missing out on was incrementing the "row" count in the code line:
ds_CodeShtText_Changed.Tables("Code_SAP").Rows(rowNum_Increment - 1).Item("Code_Descrip") = var_CodeShtText_Changed
So every time the user changes data in the particular cell, rows number in incremented by "1" and is collected in the dataset.