MS Access Basic Stock Take - ms-access

Can anyone please point me in the right direction. I have created a Table with a primary key as auto number, barcode, and qty. Barcode field is set to no duplicates allowed. I have Created a form with barcode only which i will be using a barcode scanner to read the barcodes. The question I have is if i scan a barcode that has already exists is there anyway to add +1 to the qty field instead of getting the error of it being duplicate.
The idea is just to scan every single item and it will calculate the stock for you.

A barcode scan is equivalent of entering barcode data using a keyboard and then pressing Enter. So, add to the form a button, set Default property of the button to Yes and add to your button OnClick handler something like this:
Private Sub cmdScan_Click()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("select * from Barcodes where Barcode='" & Me.txtBarcode & "'")
With rst
If .RecordCount = 0 Then
.AddNew
!Barcode = Me.txtBarcode.Value
!Qty = 1
Else
.Edit
!Qty = !Qty + 1
End If
.Update
End With
rst.Close
Set rst = Nothing
End Sub

Related

How can I auto select items in a multiselect listbox related to values in a table?

So far I have accomplished the opposite of this - I have entered records into a table based on selections made in the multiselect listbox. The multiselect listbox was named "lstboxColor" and the table was named tblColors. This was accomplished with the following code:
Set rs = New ADODB.Recordset
Dim itm As Variant
cnnLocal.CursorLocation = adUseClient 'avoid error 3705
'SET UP A LOOP TO ADD A COLOR RECORD IN THE COLORS TABLE FOR EACH SELECTED COLOR
rs.Open "tblColors", cnnLocal, adOpenDynamic, adLockOptimistic
For Each itm In lstboxColor.ItemsSelected
rs.AddNew
rs!CrayonID = CrayonID_HOLD
rs!ColorID = lstboxColor.ItemData(itm)
rs.Update
Next
rs.Close
So, now I would like to do the reverse of this - I need to open a form with a listbox and have the items in the multiselect listbox automatically be pre-selected based on the values in the table. Just can't seem to figure out the method needed other than looping through the table and using an if statement and .Selected = true. Any ideas? Thank you in advance.
EDIT: Added current code that is almost working. Changed what I need to do - I am using 3 tables and having a combobox selection auto select rows in the listbox. The code is going in the combobox's AfterUpdate(). I added "Else: If rs.NoMatch Then .Selected(i) = False" - but that's not the problem. Thinking it may be the SQL query, but that same query pulls up the correct answer in a Subform, so I know the query works. Could it be the query that I am using for the listbox rows (which is only selecting EquipmentID and Equipment name from tblEquipment)? Please let me know what you think about the reason why it may only be highlighting one row in the listbox.
EDIT: This code is working for 3 tables. The selection in the combobox auto selects the correct rows in the listbox. Solution is marked for the 2 table version(or one table), but same concept.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Dim strSQL As String
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT tblMusicEquipment.EquipmentID, *
FROM tblMusic
INNER JOIN (tblEquipment INNER JOIN tblMusicEquipment
ON tblEquipment.EquipmentID = tblMusicEquipment.EquipmentID)
ON tblMusic.InstrumentID = tblMusicEquipment.InstrumentID
WHERE (((tblMusic.InstrumentID))) = " & [cboSelectInstrument])
With Me.LstEditEquip
For i = 0 To .ListCount - 1
rs.FindFirst "EquipmentID =" & .ItemData(i)
If Not rs.NoMatch Then
.Selected(i) = True
Else: If rs.NoMatch Then .Selected(i) = False
End If
Next
End With
Have to loop through listbox and compare to a value to determine selection. In your case, do a FindFirst on recordset. If match found then select item. Assuming value to match is a numeric key, consider:
Dim rs As DAO.Recordset, i As Integer
Set rs = CurrentDb.OpenRecordset("SELECT ColorID FROM tblColors WHERE CrayonID_HOLD=" & Me.CrayonID_HOLD)
With Me.lstboxColor
For i = 0 To .ListCount - 1
rs.FindFirst "ColorID = " & .ItemData(i)
.Selected(i) = Not rs.NoMatch
Next
End With

delete duplicate records from query vba

I have built a query for duplicate records. I need to run some VBA code to check if the record is already in the table (query). If so, I need to delete the last record.
My form consists of a button with a value so that when you click the button, the data is insert into table
Dim qry As QueryDef
Set qry = CurrentDb.QueryDefs("duplicate records")
'which method do i use to see if the query got duplicate record'
With rstCategories
.MoveLast 0
End With
With rstCategories
rstCategories.Delete
End With
MsgBox "The problem already reported before!"
What I would do is run a quick query on your table:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset ("SELECT * FROM MyTable WHERE MyValue = '" & Me.MyValue & "'")
If rec.EOF = true then
'No match found, so the value isn't in the table. Add it.
Else
'Match found. This value is already in the table.
MsgBox "This value is already in the table"
End If
ok i solved it
i created a query using the find duplicates query wizard.
then i insert this code in "form close"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = Application.CurrentDb
Set rs = db.OpenRecordset("qry_Duplicates")
Do Until rs.EOF
rs.MoveFirst
rs.delete
rs.Close
Set rs = Nothing
Set rs = db.OpenRecordset("qry_Duplicates")
Loop
this code delete the entire row

RecordSet and Ms Access 2007

I have a query that has CustID with mutiple Business affiliated with the CustID. I can't use Dlookup because it only returns one variable. I want to show on a form that for this custID, here are all the businesses it's affiliated it. I want the Businesses to show up into a field (business) in another table on the form.
I started out by this
Public Sub OpenRecordset()
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Q:businesses")
Do While Not rs.EOF
T:Custinfo!business = NAME (I am lost in between on how to identify the custid and place the businesses into the table field as a Dlookup)
rs.movenext
Loop
rs.Close
Set rs = Nothing
db.Close
End Sub
I keep looking at other examples but can't seem to tie together where the dlookup replacement will take place and how will you have to put this on a form as a datasheet?
You don't need a DLookup. You could do one of two things:
1) Use a listbox and set the recordsource equal to your query (assuming Q:businesses has been appropriately defined to give the businesses as a result)
2) Still need your query to be appropriate, but you could create a string with all of the businesses in it:
Public Sub OpenRecordset()
Dim db As Database
Dim rs As Recordset
Dim StrBusinesses As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qryBusinesses")
If rs.EOF and rs.BOF Then
MsgBox("No businesses exist for this Customer")
Exit Sub 'Or do whatever else you want if there are no matches
Else
rs.MoveFirst
End If
StrBusinesses = ""
Do While Not rs.EOF
StrBusinesses = StrBusinesses & rs!Business & ", "
rs.movenext
Loop
rs.Close
StrBusinesses = Left(StrBusinesses, Len(StrBusinesses) - 2)
Forms!MyForm.MyField = StrBusinesses 'Set the field equal to the string here
Set rs = Nothing
db.Close
End Sub
Of course this assumes that the query "Q:Business" is defined to get the appropriate info such as:
SELECT custID, business FROM tblBusinesses WHERE custID = X
where "X" is the custID you are looking for.
If you need to set the query dynamically, you will need to set a querydef.
EDIT to include querydef code***********************
Also changed the name of the query to "qryBusinesses" in the code above and below as I'm not sure whether you can make a query with a colon in it.
To set the querydef, put this at the beginning of the code:
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("qryBusinesses")
qdf.SQL = "SELECT custID, business FROM tblBusinesses" _
& " WHERE custID = " & Forms!MyForm.CustID 'replace with actual form and field
This assumes that i) qryBusinesses exists already,
ii) custID is a number field
EDIT**************
If you define the query to look at the form itself, you would not need to set the sql, so if the query were defined (either in VBA or through the query wizard) as:
qdf.sql = "SELECT custID, business FROM tblBusinesses" _
& " WHERE custID = Forms!MyForm.CustID"
then you would not need to redefine the sql. However, it is a bit more dynamic to put the custID into the qdf itself as it is easier to debug any issues as you can see the exact sql that is being run in the original method.

how to reference array variable in vba access to another variable

hi guys im trying to take the value of data(0)
and put it into say variable InvoiceNumber. I tried putting an image of my watch screen but i ended up not being allow. but here is what my watch screen would look like.
data
data(0)
data(0,1) 1
data(0,2) 2
data(0,3) 3
I have tried
dim InvoiceNumber as variant <br/>
invoiceNumber = data(0)
but i keep getting error. I dont know how to reference just the part of that array. any help would be greatly appreciated.
here is the full code for anyone that would like to see a little more.
Dim db As Database
Dim rs As Recordset
Dim data As Variant
Dim colummn1 As Variant
Dim obj As Object
Set db = CurrentDb
Set rs = db.OpenRecordset("select * from Table1")
'set obj = new object
While Not rs.EOF
'MsgBox (rs.RecordCount)
'MsgBox (rs.Fields.Count)
data = rs.GetRows(rs.Fields.Count)
Column1 = data.data(0)
Wend
rs.Close
db.Close
End Sub
Try
Column1 = data(0,0)
instead of
Column1 = data.data(0)
data contains a two-dimensional array. The fist index is the field number, the second index is the row number. Both start at zero. So data(0,0) is the first field of the first row. data(1,0) is the second field of the first row.
I would try to make an array of invoices by using a user defined type
Public Type Invoice
Nr As Variant
IssueDate As Variant
Total As Variant
'Or whatever your invoice contains
End Type
Public Sub TestGetRecords()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim data As Variant
Dim numRecords As Long, i As Long
Dim Invoices() As Invoice
Set db = CurrentDb
Set rs = db.OpenRecordset("select * from Table1")
data = rs.GetRows()
rs.Close
db.Close
numRecords = UBound(data, 2) + 1
ReDim Invoices(0 To numRecords - 1) As Invoice
For i = 0 To numRecords - 1
Invoices(i).Nr = data(0, i)
Invoices(i).IssueDate = data(1, i)
Invoices(i).Total = data(2, i)
Next i
Debug.Print Invoices(0).Total
End Sub
An error in you solution is, that you placed GetRows in a loop. However, GetRows returns all the rows at once!
I am finding it a little hard to understand what you are trying to achieve.
You are reading an entire table of data into a recordset object, loading the fields of the recordset object into a two dimensional array and then trying to iterate through the array to output the results to a single variable (or a sum of array values).
Why don't you just use an SQL expression to extract the data directly from your table?
If we can assume you have a table of invoices (say ... "myInvoiceTable") with at least the following fields ...
invoiceID
invoiceValue
(and probably many others, eg. invoiceDate, clientID, etc.)
you can write an expression something like this
"SELECT SUM(invoiceValue) AS MyTotal FROM myInvoiceTable WHERE invoiceID > xxxx"
This might go into your VBA code like this.
Dim rst as Recordset
Dim strSQL as string
strSQL = "SELECT SUM(invoiceValue) AS MyTotal FROM myInvoiceTable WHERE invoiceID > 400" '-- use an appropriate WHERE clause if needed
Set rst = CurrentDb.Openrecordset(strSQL) '-- the rst object should contain a single value "MyTotal" which is the sum of the fields you are trying to access
MsgBox rst!MyTotal '-- use this value wherever you need to use it

How can I preform operations on rows in a Tabular form in MS access?

I have a Tabular type Form based upon a SELECT * FROM table type of query in MS access.
I would like to:
check / select rows in this form
iterate through the checked / selected rows
You can use the recordset or the recordsetclone.
With Me.Recordsetclone
.MoveFirst
Do While Not .EOF
MsgBox "Field 1 is: " & .Fields(1)
.MoveNext
Loop
End with
You can also allow the user to highlight records and work with those (http://support.microsoft.com/kb/208502).
Function DisplaySelectedCompanyNames()
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
'' Get the form and its recordset.
Set frm = Forms![Customers]
Set rs = frm.RecordsetClone
'' Move to the first record in the recordset.
rs.MoveFirst
'' Move to the first selected record.
rs.Move frm.SelTop - 1
'' Enumerate the list of selected records presenting
'' the CompanyName field in a message box.
For i = 1 To frm.SelHeight
MsgBox rs![CompanyName]
rs.MoveNext
Next i
End Function