Automatically Increment Number by Group and Entry - vba

I am currently trying to create a number that increases per entry and group in my table via a form.
The form is rather simple it utilizes a ComboBox to select the group from a different table. The generated number should start at 1 and increase for every new entry separately for each selected group.
Code:
My code attempts to create the number BeforeUpdate by searching for the DMax() of the variable for the group selected via ComboBox. Unfortunately, in this current state, the code does not increment the variable called Nummer but it does not throw an Error either.
Private Sub Nummer_BeforeUpdate(Cancel As Integer)
Nummer = Nz(DMax("[Nummer]", "Bau-Tagesbericht", "[Baustelle] = Kombinationsfeld354.Value")) + 1
End Sub
Variables:
Group: Baustelle
Variable that should be incremented: Nummer
Name of the Comobox: Kombinationsfeld354
I appreciate any kind of help, thank you!

You need to concatenate the variable - and set 0 (zero) for Null:
Nummer = Nz(DMax("[Nummer]", "Bau-Tagesbericht", "[Baustelle] = " & Kombinationsfeld354.Value & ""), 0) + 1
If text value, use single quotes:
Nummer = Nz(DMax("[Nummer]", "Bau-Tagesbericht", "[Baustelle] = '" & Kombinationsfeld354.Value & "'"), 0) + 1

Related

Suffix Field Values based on number of occurrences in Table

I have a table in MS Access that has a field layout_desc. I need to create a query that changes each value in this field by adding how many times the value is repeated in the table.
For example:
Thank you.
Without a primary key:
Assuming that your table does not contain a primary key field by which to sort records using a domain aggregate function, one possible method is using Static variables in VBA.
Open the VBA IDE using Alt+F11
Insert a new Public Module Alt+I,M
Copy the following basic code into the new Module:
Function Occurrence(Optional strVal As String) As Long
Static lngTmp As Long
Static strTmp As String
If strTmp = strVal Then
lngTmp = lngTmp + 1
Else
lngTmp = 1
strTmp = strVal
End If
Occurrence = lngTmp
End Function
In MS Access, create a new query with the following SQL, changing YourTable to the name of your table:
update (select t.layout_desc from YourTable as t order by t.layout_desc) q
set q.layout_desc = q.layout_desc & occurrence(q.layout_desc)
With a primary key:
If your table were to include a primary key, say id, of Long Integer data type, you could use the domain aggregate function DCount in the following way:
update YourTable t
set t.layout_desc = t.layout_desc &
dcount("*","YourTable","layout_desc = '" & t.layout_desc & "' and id <= " & t.id)

ComboBox max records reached

I have a table in MySQL accessed through a linked table (via ODBC) in Microsoft Access 2013.
This table contains over 124,000 records and I need a ComboBox in a form to be able to search through the UPC column.
This is the query that is the current datasource for the ComboBox:
SELECT [ID], [UPC_Case], [Description] FROM itemlist ORDER BY [UPC_Case];
This works perfectly except that the table view under the ComboBox won't go past record number 62287 (however the auto-fill still works for records that the table can't see), is there a way to make it able to view all the records?
Access ComboBoxes have a maximum record count of 65535.
To circumvent this, I found an article that gave me the groundwork required to write a function that sets the rowSource dynamically once a certain number of characters have been typed.
This is the function that sets the rowSource. I refactored the code so that it can be used on any comboBox in any Form with any Query.
Dim inputStub As String
Function ComboLimiter(targetCombo As ComboBox, minChars As Integer, Query As String, searchField As String)
Dim inputStr As String: inputStr = targetCombo.Text 'Set input string
Dim newStub As String: newStub = Nz(Left(inputStr, minChars), "") 'Set first n characters of targetCombo.Text
If newStub <> inputStub Then 'If first n chars are the same as previously, do nothing.
If Len(newStub) < minChars Then
'Remove the RowSource
targetCombo.RowSource = Query & " WHERE (False);"
inputStub = ""
Else
'New RowSource
targetCombo.RowSource = Query & " WHERE (" & searchField & " Like """ & newStub & "*"") ORDER BY " & searchField & ";"
inputStub = newStub
End If
End If
End Function
And the function can be bound to the ComboBox change event like this:
Private Sub UPCCombo_Change()
Call ComboLimiter(Me.UPCCombo, 1, _
"SELECT ID, UPC_Case, Description FROM itemlist", "UPC_Case")
End Sub
There's a known bug where there are sometimes issues with large recordsets like this. Are you sorting on a text based field? Try removing the sort if so and seeing if it fixes the issue.

Adding +1 in a auto increment value in SQL using vb

I want to generate an id in my vb program it will get the latest auto incremented number and will add one but my code don't work am I missing some thing or am I doing it wrong? please help me! THANKS!
Here is the code:
OpenServer()
Dim num As Integer
Dim num1 = num + 1
Newdataset("SELECT MAX(IDnum) AS IDnum FROM addnewemployee WHERE IDnum = '" & num & "' ")
txtEmpNumber.Text = "" & num1 & "." & dtpdatehired.Value.ToString("yyyyMMdd") & ""
Your method has several flaws.
First, your SQL statement should be:
SELECT MAX(IDnum) AS IDnum FROM addnewemployee
This will get the highest IDnum from the addnewemployee table. The problem with this is that if a higher record has been created and deleted. Lets say that you get a return value of 55. It is possible a record with an id of 56 has been created and deleted.
The query you really want to use is
SHOW TABLE STATUS WHERE `Name` = 'addnewemployee'
Where will get a column named Auto_increment. Get that value from the dataset, then increment it.
But it is a bad idea to do this.
If you have multiple, dependant insert, you shouild either retrieve the id at the time of insert and store that in a variable. Or better still do everything in one statement or stored procedure.

How to use a query as a control source for a textbox on a form?

I have a form myForm that's binded to a table tbl in my database. (I don't know if binded is the correct term, but It shows records from tbl on by one.)
In the form:
contact: textbox, binded to tbl.contact.
dailyCount: textbox, should show the amount of contacts entered today.
In the table:
contact
dateEntry
The query I want to use is:
SELECT count(*)
FROM tbl
WHERE contact = currentContact
AND month(dateEntry) = month(now)
AND day(dateEntry) = day(now)
AND ear (dateEntry) = year(now)
Where currentContact is the contact that is showing on the form now.
I tried putting the query in the dailyCount dataSource, but It's not working. When I click on the three dots on datasource to access the wizard, I get a window to build functions and not queries.
How do I get the currentContact showing on the form into the query?
There are multiple ways to do this. For a couple of reasons, I don't like to hardcode queries in the datasource of a specific field, and I mostly build/assign all my queries in VBA. So here's how I would do it.
In the load event of you form :
Private Sub Form_Load()
Dim SQL As String
Dim RST As Recordset
dim theCOntact as string ' Change accordingly
theCOntact = Me.currentContact ' I don't know how your fields are named, so change accordingly
SQL = "SELECT count(*) AS cnt FROM tbl WHERE contact = " & theContact & "' AND month(dateEntry) = month(now) AND day(dateEntry) = day(now) AND Year(dateEntry) = year(now)"
Set RST = CurrentDb.OpenRecordset(RST)
If RST.BOF Then
dailyCount.Value = RST!cnt
Else
dailyCount.Value = 0
End If
End Sub
Assuming your contact field is string, if its a number remove the quotes in the SQL
Probably the simplest approach is to use the DLookup function with an associated query:
Create and save a named query with your SQL code or equivalent. Let's call it "qryDailyCount". Note that it should be modified to look something like this (in particular, name the column and change the record reference to a GROUP BY for now):
SELECT count(*) as DailyCount
FROM tbl
WHERE month(dateEntry) = month(now)
AND day(dateEntry) = day(now)
AND year (dateEntry) = year(now)
GROUP BY contact
In the dailyCount textbox, set the Control Source to something like this:
=DLookUp("Count";"qryDailyCount";"contact = [contact]")
(Note that if the contact field is a text field, you must enclose it with single quotes.)

Create Variable or Method to use value in multiple forms

Fairly new to VBA. I have a list box on a form within Access which is populated with data from a table. Selecting a value from the list box gives and ID which is then used to perform a query. I need this ID to be available for use in another form to perform a query based on the Value. What is the best way of achieving this?
`Dim IDValue As String
IDValue = Me.lstBoxCompanyName.Value
CompDetailSQL = "SELECT * FROM Companies WHERE Companies.CompanyID = " & IDValue`
In a module
Dim IDValue AS Integer
Sub setIDValue(id As Integer)
IDValue = id
End Sub
Function getIDValue() As Integer
getIDValue = IDValue
End Function
Then from any of your code
'This will set you ID
setIDValue YOUR_VALUE
'This will retrieve the value
getIDValue
'so your code could be
setIDValue Me.Me.lstBoxCompanyName.Value
CompDetailSQL = "SELECT * FROM Companies WHERE Companies.CompanyID = " & getIDValue
Obviously this could use some Error Handling in the event that No IDValue is set. Also I used integer even though I see you are using a String the data type should be the same type as CompanyID so you can change Integer to String if needed but your will also have to change your query to SELECT * FROM Companies WHERE Companies.CompanyID = '" & getIDValue & "' because your query implies a number currently.
You can gain access to another forms controls by specifying the full path. Example :
Forms!MyFormsName!MyControlsName.Value
and
Forms!frmCustomer!CboCustomer.Column(0)
So if you want to access a value in VBA that is contained in another form and use it as part of a query it would look like:
CompDetailSQL = "SELECT * " & _
"FROM Companies " & _
"WHERE CompanyID = " & Forms!frmCustomer!lstBoxCompanyName.Value
in ms Access you can also simply use form_NAMEofFORM.NAMEofCONTROL
Example you can reference
dim myAmount as double
myAmount = form_receipt.total
to access the value showing on form receipt textbox total.