How to get the last record id of a form? - vba

I currently have a form in access.
What I want to do is get the value of the last record added.
For example, if i have 10 records, I want to get the value "10", because this is the id of the added last record. I am trying to run a query with the function last id inserted() but it is not working.
This the code I am using :
Dim lastID As Integer
Query = "select last_insert_id()"
lastID = Query
MsgBox (lastID)
What am I missing?

There is a function DMax that will grab the highest number.
Dim lastID As Integer
lastID = DMax("IDField","YourTable")
' or = DMax("IDField","YourTable","WhenField=Value")
MsgBox lastID
The other Domain functions are:
DAvg
DCount
DFirst
DLast
DLookup
DMin
DStDev
DStDevP
DSum
DVar
DVarP
Check with your friendly F1 key for more info

Following on from the last comments, here's a piece of code I used recently to turn the last ID value of a record set into variable for use in VBA. It's not great, however, because I still can't work out how to turn the record's ID field value directly into a variable. Instead I used the inelegant solution of copying the record set into an excel workbook, and then setting the variable value to the value of the cell I just copied into.
EDIT: Worked out how to turn the ID into a simple variable: new code at end
This is all run from a single client workbook:
Option Explicit
Public AftUpD As Long
Public BfrUpD As Long
Sub AssignLstRowAftUpD2()
Dim dbPP As DAO.Database
Dim ResTemp As DAO.Recordset
Dim z As Long
Dim SelectLast As String
SelectLast = "SELECT Max(Table1.ID) AS MaxOfID FROM Table1"
'Debug.Print SelectLast
Set dbPP = OpenDatabase("C:\filepath\Database11.mdb")
Set ResTemp = dbPP.OpenRecordset(SelectLast)
If ResTemp.EOF Then
GoTo EndLoop
End If
Worksheets("Diagnostics").Visible = True
Worksheets("Diagnostics").Range("C4").CopyFromRecordset ResTemp
z = Sheets("Diagnostics").Range("C4").Value
Sheets("Diagnostics").Visible = False
AftUpD = z
'Debug.Print AftUpD
EndLoop:
ResTemp.Close
dbPP.Close
Set dbPP = Nothing
Set ResTemp = Nothing
'Set SelectionLast = Nothing
'z = Nothing
End Sub
Then I used this value as a variable to make a new SQL query:
Sub Query()
'This query uses the highest ID value in a companion spreadsheet (the public
'variable BfrUpD), which is set in a sub I haven't posted here, to find out
'how many records have been added to the database since the last time the
'spreadsheet was updated, and then copies the new records into the workbook
'Be warned: If you run this query when BfrUpD is equal to or greater than AftUpD it
'will cause a crash. In the end user version of this, I use several If tests,
'comparing BfrUpD with other public variables, to make sure that this doesn't
'happen.
Dim WBout As Excel.Workbook, WSout As Excel.Worksheet
Dim dbPP1 As DAO.Database
Dim qryPP1 As DAO.Recordset
Dim ResTemp1 As DAO.Recordset
Dim TestValue As String
Dim strSQL2 As String
TestValue = BfrUpD
'Debug.Print TestValue
strSQL2 = "SELECT * FROM Table1 WHERE (((Table1.ID)>" & TestValue & "))"
'Debug.Print strSQL2
Set dbPP1 = OpenDatabase("C:\filepath\Database11.mdb")
Set qryPP1 = dbPP1.OpenRecordset(strSQL2)
Set WBout = Workbooks.Open("C:\filepath\h.xlsm")
Set WSout = WBout.Sheets("sheet1")
WSout.Range("A1").End(xlDown).Offset(1, 0).CopyFromRecordset qryPP1
qryPP1.Close
dbPP1.Close
WBout.Save
WBout.Close
MsgBox "Data copied. Thank you."
Set WBout = Nothing
Set WSout = Nothing
Set dbPP1 = Nothing
Set qryPP1 = Nothing
Set ResTemp1 = Nothing
End Sub
EDIT: Code for getting field value directly into variable
Dim dbPP As DAO.Database
Dim ResTemp As DAO.Recordset
Dim z As Long
Dim SelectLast As String
SelectLast = "SELECT Max(Table1.ID) AS MaxOfID FROM Table1"
'Debug.Print SelectLast
Set dbPP = OpenDatabase("C:\filepath\Database11.mdb")
Set ResTemp = dbPP.OpenRecordset(SelectLast)
z = ResTemp(0) 'specifying it's array location (I think) - there is only one
'item in this result, so it will always be (0)
AftUpD = z
'Debug.Print AftUpD
ResTemp.Close
dbPP.Close
Set dbPP = Nothing
Set ResTemp = Nothing
'Set SelectionLast = Nothing
'z = Nothing
End Sub

What you would do is set up and save a query that gets the value for you first. Call it MaxID
e.g
SELECT Max(ID) as result FROM Your_Table_Name
Then, in your VBA code, set your variable to that
eg.
Dim IDresult As Integer
IDresult = DLookup("[result]", "MaxID")
MsgBox(IDresult)

Related

Nested dictionary object not found

I am attempting to take some table, a list of fields to group by, and a number field as input, to produce an output table that contains the group-by fields and medians for the number field.
I have this working for a single field to group by, but not multiple fields.
I am using nested dictionaries to eventually aggregate my grouped-field values and their medians.
When I try to access the inner dictionary I get a
'424' Run-time error: Object required
Once I put the inner dictionary in the outer dictionary I cannot access it like one would suspect.
Public Sub MedianByGroups(ByVal inputTable As String, ByVal strField As String, _
ByVal strGroup As String, ByVal outputTable As String, _
Optional ByVal strCriteria As String)
DoCmd.SetWarnings False
Dim db As DAO.Database: Set db = CurrentDb()
Dim rstDomain As DAO.Recordset
Dim strSQL As String
Dim varMedian As Long: varMedian = 0
Dim intRecords As Long
Dim splitgroup As Variant
splitgroup = Split(strGroup, ", ")
strSQL = "SELECT DISTINCT " & strGroup & " INTO TMP_GROUPINGTABLE FROM " & _
inputTable & ";"
DoCmd.RunSQL (strSQL)
Set rstDomain = db.OpenRecordset("TMP_GROUPINGTABLE")
Dim i As Integer: i = 0
Dim group_dict As New Dictionary
Dim tmp_dict As Dictionary
Set tmp_dict = New Dictionary
If Not (rstDomain.EOF And rstDomain.BOF) Then
rstDomain.MoveFirst
Do Until rstDomain.EOF = True
For Each Label In splitgroup
tmp_dict.Add Label, rstDomain.Fields(Label).Value
Next Label
group_dict.Add i, tmp_dct
Set tmp_dict = New Dictionary
i = i + 1
rstDomain.MoveNext
Loop
End If
rstDomain.Close
For Each key In group_dict.Keys
For Each addlKey In group_dict(key).Keys
Debug.Print addlKey, group_dict(key)(addlKey)
Next addlKey
Next key
DoCmd.SetWarnings True
end sub
If one had a table, called "MYTABLE", that looked like this:
GROUP1 GROUP2 VAL1
A C 400
B D 500
One would call this sub like:
call MedianByGroups("MYTABLE", "VAL1', "GROUP1, GROUP2", "OUTPUTTABLE")
This doesn't do the Median or any other parts yet, as I am hitting this stumbling point with the dictionaries.
Where it fails is the inner for loop on that nested for loop near the end.
For Each addlKey In group_dict(key).Keys
Edit: As ComputerVersteher pointed out, the issue was a spelling mistake I had made. The solution to avoid this as Andre mentioned is to add the option explicit to the module.
Not a complete answer -- I set up an example without the MS Access-specific parts and the logic in your code seems to run fine. As near as I can tell, there's no real difference in the logic below and your code above. Does this example run for you?
Option Explicit
Sub TestingNestedDictionaries()
Dim group_dict As New Dictionary
Dim tmp_dict As Dictionary
Set tmp_dict = New Dictionary
Dim labels As Variant
labels = Array("red", "orange", "yellow", "green", "blue", "indigo", "violet")
Dim i As Long
For i = 1 To 5
Dim label As Variant
For Each label In labels
tmp_dict.Add label, label & "-" & i
Next label
group_dict.Add i, tmp_dict
Set tmp_dict = New Dictionary
Next i
'--- pull out one color
' Const THIS_COLOR As String = "green"
' Dim group As Variant
' For Each group In group_dict
' Dim subDict As Dictionary
' Set subDict = group_dict(group)
' Debug.Print subDict(THIS_COLOR)
' Next group
Dim key As Variant
Dim addlKey As Variant
For Each key In group_dict.Keys
For Each addlKey In group_dict(key).Keys
Debug.Print addlKey, group_dict(key)(addlKey)
Next addlKey
Next key
End Sub

How to check if quantity is more or less on a table?

I need to check if I have enough quantity of a specific product. How do I make it so the quantity that it checks is the one of the product in a textbox(txtIdProd).
This is my code:
Private Sub Cantidad_LostFocus()
Set myDatabase = CurrentDb()
Set myRs = myDatabase.OpenRecordset("TblProductos", dbOpenDynaset)
Dim cant As Integer
cant = txtCantidad
myRs.FindFirst "IDProducto=" & Me.txtIdProd
If myRs.NoMatch = False Then
If cant > myRs("CantidadDisponible") Then
Me.Label27.Visible = True
End If
End If
Exit Sub
Me.Label27.Visible = False
End Sub
The FindFirst code should do exactly what you want. However, probably more efficient to filter the recordset.
Private Sub Cantidad_LostFocus()
Dim cant As Integer
If Not IsNull(Me.txtIDProd) Then
Set myDatabase = CurrentDb()
Set myRs = myDatabase.OpenRecordset("SELECT CantidadDisponible FROM TblProductos WHERE IDProducto=" & Me.txtIdProd, dbOpenDynaset)
cant = rs!CantidadDisponible
End If
Me.Label27.Visible = Me.txtCantidad <= cant
End Sub
Another approach that doesn't use recordset:
Me.Label27.Visible = Me.txtCantidad <= Nz(DLookup("CantidadDisponible", "TblProductos", "IDproducto=" & Nz(Me.txtIDProd,0)), 0)
Note use of <= and assumes txtCantidad will not have 0.

Excel VBA insert page break every nth row

I have the below code which does everything except insert the page break after row 35. can anyone help please?
Sub PrintSet()
Dim rs As Worksheet
Dim sPrintArea As String
sPrintArea = "A1:AE65"
For Each rs In Sheets
rs.PageSetup.Orientation = xlLandscape
rs.PageSetup.Zoom = False
rs.PageSetup.FitToPagesWide = 1
rs.PageSetup.FitToPagesTall = 2
rs.PageSetup.PrintArea = sPrintArea
rs.HPageBreaks.Add before:=Range("A36")
Next rs
End Sub
Rewrite the last line like this:
rs.HPageBreaks.Add before:=rs.Range("A36")
Thus, you are adding a reference rs also. It should work better. See what Microsoft says about referencing ranges:
https://msdn.microsoft.com/en-us/library/office/aa221547(v=office.11).aspx
Try something like this:
Dim hpgbr As HPageBreak
Dim hpgbrs As HPageBreaks
Set hpgbr = hpgbrs.Add(Before:=Range("A36"))
This works for me. I removed the Zoom
Sub PrintSet()
Dim rs As Worksheet
Dim sPrintArea As String
sPrintArea = "A1:AE65"
For Each rs In Sheets
rs.PageSetup.Orientation = xlLandscape
rs.PageSetup.FitToPagesWide = 1
rs.PageSetup.FitToPagesTall = 2
rs.PageSetup.PrintArea = sPrintArea
rs.HPageBreaks.Add before:=Range("A36")
Next rs
End Sub

Excel VBA Run Time Error '424' object required

I am totally new in VBA and coding in general, am trying to get data from cells from the same workbook (get framework path ...) and then to start application (QTP) and run tests.
I am getting this error when trying to get values entered in excel cells:
Run Time Error '424' object required
I believe I am missing some basic rules but I appreciate your help. Please see below the part of code in question:
Option Explicit
Private Sub RunTest_Click()
Dim envFrmwrkPath As Range
Dim ApplicationName As Range
Dim TestIterationName As Range
'Dim wb As Workbook
'Dim Batch1 As Worksheets
Dim objEnvVarXML, objfso, app As Object
Dim i, Msgarea
Set envFrmwrkPath = ActiveSheet.Range("D6").Value ' error displayed here
Set ApplicationName = ActiveSheet.Range("D4").Value
Set TestIterationName = ActiveSheet.Range("D8").Value
The first code line, Option Explicit means (in simple terms) that all of your variables have to be explicitly declared by Dim statements. They can be any type, including object, integer, string, or even a variant.
This line: Dim envFrmwrkPath As Range is declaring the variable envFrmwrkPath of type Range. This means that you can only set it to a range.
This line: Set envFrmwrkPath = ActiveSheet.Range("D6").Value is attempting to set the Range type variable to a specific Value that is in cell D6. This could be a integer or a string for example (depends on what you have in that cell) but it's not a range.
I'm assuming you want the value stored in a variable. Try something like this:
Dim MyVariableName As Integer
MyVariableName = ActiveSheet.Range("D6").Value
This assumes you have a number (like 5) in cell D6. Now your variable will have the value.
For simplicity sake of learning, you can remove or comment out the Option Explicit line and VBA will try to determine the type of variables at run time.
Try this to get through this part of your code
Dim envFrmwrkPath As String
Dim ApplicationName As String
Dim TestIterationName As String
Simply remove the .value from your code.
Set envFrmwrkPath = ActiveSheet.Range("D6").Value
instead of this, use:
Set envFrmwrkPath = ActiveSheet.Range("D6")
You have two options,
-If you want the value:
Dim MyValue as Variant ' or string/date/long/...
MyValue = ThisWorkbook.Sheets(1).Range("A1").Value
-if you want the cell object:
Dim oCell as Range ' or object (but then you'll miss out on intellisense), and both can also contain more than one cell.
Set oCell = ThisWorkbook.Sheets(1).Range("A1")
Private Sub CommandButton1_Click()
Workbooks("Textfile_Receiving").Sheets("menu").Range("g1").Value = PROV.Text
Workbooks("Textfile_Receiving").Sheets("menu").Range("g2").Value = MUN.Text
Workbooks("Textfile_Receiving").Sheets("menu").Range("g3").Value = CAT.Text
Workbooks("Textfile_Receiving").Sheets("menu").Range("g4").Value = Label5.Caption
Me.Hide
Run "filename"
End Sub
Private Sub MUN_Change()
Dim r As Integer
r = 2
While Range("m" & CStr(r)).Value <> ""
If Range("m" & CStr(r)).Value = MUN.Text Then
Label5.Caption = Range("n" & CStr(r)).Value
End If
r = r + 1
Wend
End Sub
Private Sub PROV_Change()
If PROV.Text = "LAGUNA" Then
MUN.Text = ""
MUN.RowSource = "Menu!M26:M56"
ElseIf PROV.Text = "CAVITE" Then
MUN.Text = ""
MUN.RowSource = "Menu!M2:M25"
ElseIf PROV.Text = "QUEZON" Then
MUN.Text = ""
MUN.RowSource = "Menu!M57:M97"
End If
End Sub

How to export to excel with specified month

Sub Initialize
'Copyright Botstation (www.botstation.com)
Dim session As New NotesSession
Dim wks As New NotesUIWorkspace
Dim db As NotesDatabase
Dim view As NotesView
Dim uiView As NotesUIView
Dim doc As NotesDocument
Dim column As NotesViewColumn
Dim row As Long,colcounter As Long,arrcnt As Long,arrcounter As Long, x As Long
Dim filename As String, currentvalue As String
Dim rowsatonce As Integer,cn As Integer
Dim xlApp As Variant, xlsheet As Variant,xlwb As Variant, xlrange As Variant, tempval As Variant
Dim DataArray
Dim VColumns List As String
ReDim DataArray(0, 80) As String
'80 columns is our expected max number of columns in the view. It's dynamically recomputed below to actual (lower) number. Change if the number of columns is larger.
Set db=session.CurrentDatabase
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'Excel program is visible (to avoid errors and see what is happening)
Set xlwb=xlApp.Workbooks.Add
Set xlsheet =xlwb.Worksheets(1)
Set uiView = wks.CurrentView
Set view = db.GetView( uiView.ViewName ) ' get the view currently open in UI
arrcnt=0
row=1
colcounter=0
rowsatonce=20
ForAll c In view.Columns
If c.isIcon<>True Then ' do not include icon columns
If c.Formula<>"""1""" And c.Formula<>"1" Then 'do not include columns which are used for counting docs (Total)
colcounter=colcounter+1
DataArray(row-1, colcounter-1) =c.Title
VColumns(CStr(cn))=CStr(cn)
End If
End If
cn=cn+1
End ForAll
ReDim Preserve DataArray(0, colcounter-1) As String
xlsheet.Range("A1").Resize(1, colcounter).Value = DataArray ' set column names
ReDim DataArray(rowsatonce-1, colcounter-1) As String
row=2
x=0
Set doc = view.GetFirstDocument
While Not ( doc Is Nothing )
ForAll col In VColumns
currentvalue=""
tempval= doc.ColumnValues(Val(col))
If IsArray(tempval) Then
ForAll v In tempval
If currentvalue="" Then
currentvalue=v
Else
currentvalue=currentvalue+","+v
End If
End ForAll
Else
currentvalue=tempval
End If
x=x+1
DataArray(arrcounter, x-1) =currentvalue
End ForAll
x=0
row=row+1
arrcounter=arrcounter+1
If arrcounter/rowsatonce=arrcounter\rowsatonce And arrcounter<>0 Then
xlsheet.Range("A"+Cstr(arrcnt*rowsatonce+2)).Resize(rowsatonce, colcounter).Value = DataArray
arrcnt=arrcnt+1
arrcounter=0
ReDim DataArray(rowsatonce-1, colcounter-1) As String
End If
Set doc = view.GetNextDocument (doc)
Wend
If arrcounter/rowsatonce<>arrcounter\rowsatonce And arrcounter>0 Then
' Redim Preserve DataArray(arrcounter, colcounter-1) As String
xlsheet.Range("A"+Cstr(arrcnt*rowsatonce+2)).Resize(arrcounter, colcounter).Value = DataArray
End If
MsgBox "Done"
End Sub
Once you have got the month that you want to include you can add a condition after this line:
While Not ( doc Is Nothing )
Compare the month (and probably year) with the (date) item on the document. You might need the NotesDateTime class to do this.
To filter the right month you can do this: (assuming you also need the year)
If year(date1) * 100 + month(date1) = year(date2) * 100 + month(date3)