Expanding a number range from Access data entry form into table - ms-access

I am building an inventory database that needs to track individual items. Each item has a unique number associated with it. However, we receive these items in bulk quantities, and distribute them likewise. I have set up a data entry form to input the start of the item number range, and the end of the item number range. For example, the item range would look something like this -- 1056-56701 through 1056-56800. The item number is always 4 digits followed by a dash and 5 digits. I am looking to expand the range that is put into my data entry form, so that each individual item number within the range is saved to my table. In this case-- 1056-56701, 1056-56702, 1056-56703, 1056-56704....156-56800. I also need to save the corresponding info for each item range as it is saved as an individual number. That would include things like 'date received', 'size', etc.
I have read some other responses to similar problems, but I still can't get this to function properly. Any thoughts?

An example of the design you might need:
MASTER
------
Prefix Int (primary key)
SuffixBegin String
SuffixEnd String
ItemDescription String
DateReceived Date
Size String
. . . more columns for any other data common to the entire "batch"
DETAIL
------
ID Int (primary key)
Prefix Int (foreign key, related to the same-named column in the Master table)
Suffix String (values such as "56701", "56702", etc.)
. . . more columns for whatever data you need to store about unique suffix items
You could then find data for a specific item something like this (pseudo-SQL, untested):
SELECT M.ItemDescription, D.SomeSuffixSpecificData
FROM MASTER M, DETAIL D
WHERE M.Prefix = (the Prefix you're interested in)
AND D.Suffix = (the Suffix you're interested in)
JOIN M.Prefix = D.Prefix

You can use a function like this to list and add your items:
Public Function AddBulk(ByVal First As String, ByVal Last As String) As Integer
Dim Items As Integer
Dim Item As Integer
Dim BulkId As String
' Open your inventory table.
' Set rs = CurrentDb.OpenRecordset("Select Top 1 * From Inventory")
For Item = Split(First, "-")(1) To Split(Last, "-")(1)
BulkId = Split(First, "-")(0) & "-" & Item
Debug.Print Items, BulkId
' Insert code to add one record to your inventory table.
' rs.AddNew
' rs!BulkId.Value = BulkId
' rs!OtherField.Value = somevalue
' rs.Update
Items = Items + 1
Next
' rs.Close
AddBulk = Items
End Function

Related

Populating and Sorting Three Columns of a DataGridView from Data in a Single MySQL Row Including Images

As the title says I'm trying to sort a DataGridView into 3 Columns using data from MYSQL. So far I managed to populate 2 of the 3 columns with the correct data using:
'Load Shift Report Data
Dim Com2 As MySqlCommand
Connection.Open()
Dim Shift_Status As String = "OPEN"
Com2 = New MySqlCommand("SELECT Item0, Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9, Item10,
Item11, Item12, Item13, Item14, Item15, Item16, Item17
FROM tbl4ShiftReport WHERE USER_ID= '" + User_ID + "' AND Shift_Status= '" + Shift_Status + "';", Connection)
Com2.CommandType = CommandType.Text
Dim MSDA2 As New MySqlDataAdapter(Com2)
Dim DT2 As New DataTable()
MSDA2.Fill(DT2)
Connection.Close()
If DT2.Rows.Count >= 1 Then
Dim DT3 As New DataTable()
For i As Integer = 0 To DT2.Rows.Count + 1
DT3.Columns.Add()
Next
For i As Integer = 0 To DT2.Columns.Count - 1
DT3.Columns("Column2").DataType = GetType(Decimal)
DT3.Columns("Column3").DataType = GetType(Byte())
DT3.Rows.Add()
DT3.Rows(i)(0) = DT2.Columns(i).ColumnName
Next
For i As Integer = 0 To DT2.Columns.Count - 1
For j As Integer = 0 To DT2.Rows.Count - 1
DT3.Rows(i)(j + 1) = DT2.Rows(j)(i)
Next
Next
DataGridView2.DataSource = DT3
This is only working because I'm only getting one set of the Data and their Column names in the db and sorting them into the Data Table and then making this the data source for the datagrid.
But the third set of data for the third column in the datagrid, which I'm not getting yet in this example, are ftp links stored as strings to display in this third column as thumbnails. These links are in a separate table in mysql and I can reference them to the corresponding data however I'm unsure on how to accommodate this into the winforms table.
With this said my first question would be how can I sort this third column into the data table? Right now I'm basically putting the column names in column 1 and the data in column 2 but this third set of data I dont know how to sort it. I'm thinking making an if statement that if the column name starts with "item" put it in column 2 and if it starts with "img" put it in column 3 but still i need to sort that item1 goes with img1 and so on.
My second question would be once I sort the data how can I download the images and display them in that third column.
Thank you in advance!

Access 2013 - sequential numbering without autonumber

New to this site and access.
I am trying to create a database to keep track of a master log. Need help with sequential numbering. I currently have the following tables.
PO Table
POID - Autonumber(PK)
PODate - Date/Time
Supplier - String
Item Table
ItemID - Autonumber(PK)
POID - Ingeter(FK)
ItemDescription - String
Quantity - Integer
MasterLog Table
MasterLogID - Autonumber(PK)
ItemID - Integer(FK)
PieceNumber - Integer ( 1,2,3 ... etc)
MFG - String
Process - String
Length - Integer
MfgIDNum - String (ABD123XTY-1234)
I am trying to automate the data entry of the PieceNumber field. So when you enter a new PO and add items to it, once received. It will add a row to the masterlog table starting at piece number 1 through how ever many pieces we have. We number the pieces based on the items we purchased.(i.e. with Item 1 we purchased 100 pieces. So I would have Item 1 piece 1 through 100.) Then we are able to add the other data to the table. Just trying to reduce some data entry time and avoid some mistakes in the numbering of this field.
Any ideas?
Something like this would be a very simple way of doing it. You'd have to have a predefined table called Numbers with integers starting from 1 to however high a quantity you might have:
INSERT INTO MasterLog (ItemID, PieceNumber)
SELECT Item.ItemID, Numbers.Number
FROM Item, Numbers
WHERE (Item.ItemID = Forms!Items!ItemID) AND
(Numbers.Number <= Item.Quantity)
ORDER BY Numbers.Number
If you wanted to add the pieces one by one you could default the PieceNumber field on the related form. Make sure you default MasterLog.ItemID as well:
=Nz(DMax("[PieceNumber]","[MasterLog]","[ItemID] = " & Forms!Items!ItemID), 0) + 1
For a VBA solution try something like this:
Dim db As Database
Dim strSQL As String
Dim frm As Form
Dim i As Integer
Set db = CodeDb()
Set frm = Forms!Items
If frm!Quantity > 0 Then
For i = 1 To frm!Quantity
strSQL = "INSERT INTO MasterLog (ItemID, PieceNumber) " & _
"SELECT " & frm!Item & " AS ItemID, " & _
i & " AS PieceNumber"
db.Execute strSQL, dbFailOnError
Next i
End If
Set db = Nothing
All of these presume a form displaying your current item called Items.

parsing the first 2 parts of a string

I have a field called 'Specimen' that has entries formatted like 'CM-Z-01', 'TOR-XY-03', etc. I want to populate 2 additional fields in the same table, called 'TestType' and 'Axis', with the first two sections of the 'Specimen' entry, respectively. So 'TestType' would have 'CM' and 'TOR'; Axis would have 'Z' and 'XY'
I can do the first one just fine, with
UPDATE MechanicalData
SET MechanicalData.TestType = Left(Specimen,InStr(Specimen,"-")-1);
Is there an easy way to grab the middle portion?
You can use the Split function which returns an array with the string parts:
firstPart = Split("TOR-XY-03", "-")(0)
secondPart = Split("TOR-XY-03", "-")(1)
Make a function that you can call in your query based on this function. You cannot use Split directly as it is not supported in SQL.
Public Function SpecimenPart(ByVal s As Variant, ByVal partNo As Long) As Variant
If Nz(s) <> "" Then
Dim parts As Variant
parts = Split(s, "-")
If partNo - 1 <= UBound(parts) Then
SpecimenPart = parts(partNo - 1)
End If
End If
End Function
The parameter partNo is one-based (1 for first part).
UPDATE MechanicalData SET
TestType = SpecimenPart(Specimen, 1),
Axis = SpecimenPart(Specimen, 2);

navigating through table records in Access VBA

I have a table with two columns (value1 and value2) the values are sorted from lowest to highest. example:
Value1
20
40
43
90
100
122
Value2
4
5
9
10
15
18
I ask the user to enter an input value and then I calculate the value of CalcFinalValue which can be calculated in one of the following:
if the user input value already exist in value1 field, then return the corresponding value of in field value2. for example if the user input is 100 then CalcFinalValue will be 15
if the user input value does not exist in value1 field, then locate the two values in value1 field that the input value is between them(for example if the input value is 42, the I want to locate 40 and 43 from value1 field). Calculate CalcFinalValue as:
CalcFinalValue=(40*9)+(43*5)/42
in other words the formula will be as:
CalcFinalValue=(LowerValue of the inbetween values *lookup value of the HigherValue of the inbetween values)+(HigherValue of the inbetween values *lookup value of the LowerValue of the inbetween values)/(user input value)
I want to perform this in Access 2007 VBA.
I hope this is clear. Thanks for your help in advance!
Dim rs AS DAO.Recordset
Set rs = CurrentDb.OpenRecordset("TableName", dbOpenTable)
' inp stores the user input value; i counts the number of records that have been accessed
Dim inp, i as Integer
' r2c1 and r2c2 store the data of the second row in case the two-row calculation is needed
Dim r2c1, r2c2 as integer
' Since I mostly use forms for I/O that's what I've used here
' Change this to whatever method you use to get the user input
inp = Forms!FormName.InputTextBoxName.Value
i = 0
rs.MoveFirst
Do While (Not rs.EOF)
i = i + 1
' Check if the user input exists in the table
If (rs.Fields("Value1") = inp) Then
' Again use whatever output method you want
Set Forms!FormName.OutputTextBoxName.Value = rs.Fields("Value1")
End Do
' Otherwise, check if the current value in column 1 is greater than the input
Else If (rs.Fields("Value1") > inp) Then
' If this is true for the first row then the input is less than the lowest number.
' I assume this is out-of-bounds, but use whatever boundary condition you want
If (i = 1) Then
MsgBox("Out of Bounds", vbOkOnly)
Else
' Save the required values from this row
r2c2 = rs.Fields("Value2")
r2c1 = rs.Fields("Value1")
' Goto previous row which and calculate according to your formula
rs.MoveLast
Set Forms!FormName.OutputTextBoxName.Value = (r2c1*r2c2 + rs.Fields("Value1")*rs.Fields("Value2"))/inp
End If
End If
rs.MoveNext
Loop
' If nothing was found, the input was larger than all values in 'Value1'
If (rs.EOF) Then
MsgBox("Out of Bounds", vbOkOnly)
End If
Substitute Value1 and Value2 with whatever column names you use

Getting the value not the ID

I have a table tblInvestigators which contains a lookup field to display a list of names
A grant may have more than 1 investigator.
A requirement of my project is to list all investigators in a single cell next to the grant details, so i'd have:
Grant A | Name A, Name B, Name C
etc.
I have a VBA module that concatenates the investigators into 1 cell as follows:
'Concat Returns lists of items which are within a grouped field
Public Function c(strID As String, strAddMe As String) As String
Static prevID As String
Static strFinal As String
Static strLastAdded As String
If (strID = prevID And strAddMe <> strLastAdded) Then
strFinal = strFinal & ", " & strAddMe
Else
prevID = strID
strLastAdded = strAddMe
strFinal = strAddMe
End If
c = strFinal
End Function
And an access query that calls it (SQL):
SELECT g.grant_id, Max(c(g.grant_id,tblInvestigators.investigator)) AS Expr1
FROM tblGrants AS g LEFT JOIN tblInvestigators ON g.grant_id = tblInvestigators.grant_id
WHERE (((g.grant_id)=6))
GROUP BY g.grant_id;
When I run this, it returns a comma separated list, but it is a list of the ID numbers from the look up column (tblInvestigators.investigator)rather than the names. How can I get the names?
Chris
It is never a good idea to use look-up fields: http://www.mvps.org/access/lookupfields.htm
It is disguising the standard way of getting the results you want, which is to use a query to join the ID to the look-up table and return the description.
Have a look at this Does MS access(2003) have anything comparable to Stored procedure. I want to run a complex query in MS acceess