Insert multiple records with a number range in MS Access - ms-access-2007

How would one add multiple records in a DB Table for numerical values. I have a table called Issue (Customer, Issue Date, Sealno.) where a user fills in the sealno. of the first seal and sealno. of the last seal. The first seal and the last is the range. In the table record of the first seal to the last will be filled all with the same Customer name and Issue Date.
My DB name is Seals;Table name Issue. The below macro is initiated by a save button in the DB Form;
Private Sub Command17_Click()
Dim rsIssue As DAO.Recordset
Dim Seal As Date
Set rsIssue = CurrentDb.OpenRecordset("Select Top 1 *
From Issue")
Seal = Me!Seal1.Value
While DateDiff("d", Seal, Me!Seal2.Value) >= 0
rsIssue.AddNew
rsIssue!SealNo.Value = Me!SealNo.Value
rsIssue!Customer.Value = Me!Customer.Value
rsIssue!Seal1.Value = Seal
rsIssue.Update
Seal = DateAdd("d", 1, Seal)
Wend
rsIssue.Close
Set rsIssue = Nothing
End Sub
The code however does not work.
Thanks in advance.

Related

Generate Records to Add to Table - Loop or Recordset? [duplicate]

Where I work we receive electronic meters from customers and try to solve the errors they have. We will receive 4-8 meters at a time that all have the same problem, same specs, same everything, the only thing different between each meter is the Serial Number. I want to be able to enter every serial number, and the common specs all in one form to create multiple records.
Here's a pic of what I have for the form. I was able to create records for just one serial number at a time, but I would like to do it all at once to make data entry quicker and easier.
Meter Entry Form
So summary, Multiple Meters, all identical specs, different serial numbers. I want to enter it all into a form and have multiple records created. Thanks for any help or insight you can provide me.
-Chris
You could bind a subform to the table that stores your meter records and then have some unbound fields on your main form that allows you to enter the information that would be repeated in your batch of records. You could also put another unbound text box on the main form to specify the number of records you want that will have this repeated information.
So in the mock-up below, you'd specify how many records you want (red box), e.g. 10 records:
Then you'd supply the data that would be repeated for these 10 records (blue boxes):
You'd then click a button that would create the number of records specified with the repeated information given:
It would then just be a case completing the unique serial number for each of the records in the batch you have generated.
Here's the VBA I used on the Add button:
Private Sub cmdAddRecords_Click()
batchAdd Me.txtRecords
Me.tblMeters_sub.Requery
End Sub
...and the batchAdd sub routine it calls:
Public Sub batchAdd(records As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Set db = CurrentDb
Set rs = db.OpenRecordset("tblMeters")
i = 1
Do While i <= records
rs.AddNew
rs!SerialNumber = ""
rs!MeterFirmware = Me.MeterFirmware
rs!MeterCatalog = Me.MeterCatalog
rs!Customer = Me.Customer
rs!MeterKh = Me.MeterKh
rs!MeterForm = Me.MeterForm
rs!MeterType = Me.MeterType
rs!MeterVoltage = Me.MeterVoltage
rs.Update
i = i + 1
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
Here's a link to the mock-up (if you want a closer look).
Update
In response to your query about whether the subform could be filtered so it doesn't just become a big list of all meters, you could add another field to the tblMeters table that will take the date and time that you added records to the table:
You'd then need to add another line to the batchAdd sub that will put the system time and date in this new field:
...
Do While i <= records
rs.AddNew
rs!SerialNumber = ""
rs!MeterFirmware = Me.MeterFirmware
rs!MeterCatalog = Me.MeterCatalog
rs!Customer = Me.Customer
rs!MeterKh = Me.MeterKh
rs!MeterForm = Me.MeterForm
rs!MeterType = Me.MeterType
rs!MeterVoltage = Me.MeterVoltage
rs!DateAdded = Now ' <-- HERE!
rs.Update
i = i + 1
Loop
...
You'll then need to change the subform's Record Source property (Design View > select subform > Property Sheet > Data tab > Record Source):
Put the following SQL in there:
SELECT TOP 15 tblMeters.SerialNumber, tblMeters.MeterFirmware, tblMeters.MeterCatalog,
tblMeters.Customer, tblMeters.MeterType, tblMeters.MeterForm, tblMeters.MeterKh,
tblMeters.MeterVoltage, tblMeters.DateAdded
FROM tblMeters
ORDER BY tblMeters.DateAdded DESC;
... which will order the records by the date/time field (most recent at the top) and then show only the first 15 of these records. If you want a different number of records change the TOP 15 bit to a different number of your choosing.
When you click "Add", your new batch of records should be added to the top of the list and the list should stay at a maximum of 15 records (or whatever number you specify in TOP ...)
Be aware that when I was testing this, clicking the "Add" button rapidly a few times seemed to cause the sql to not bother with the TOP ... filter, but as long there's like a second or more between each "Add" click it seemed to work fine.
Hope this helps.

Create Sequence Number by year and number that will increase +1

I would like to edit/modify my field based on year and then the sequence number.
Field: Year - 00#
For example, a file is created this year, 2020.
So, I would like a filed that is computed by number and will increase +1.
The filed will be : 2020-001 , then 2020 - 002 and so on.
Before, I got some code from somebody to generate a sequence number.
Sub Querysave(Source As Notesuidocument, Continue As Variant)
'get total no of documents from the view
'if no documents then seq start with 1
Dim docCount
Dim viewCollection As NotesViewEntryCollection
Dim viewEntry As NotesViewEntry
Dim vNum As Long
If source.IsNewDoc Then
Set viewCollection = view.AllEntries
docCount = viewCollection.count
If doccount = 0 Then
doccount=1
Call source.FieldSetText("ReqNo","REQ" & Cstr(Format(docCount,"00000#")))
Else
Set viewEntry = viewCollection.GetLastEntry
Call source.FieldSetText("ReqNo","REQ" &Cstr(Format(doccount+1,"00000#")))
End If
End If
End Sub
Based on that code, how can I edit the "REQ" as per above to be the created year?
Thank you very much for your help.
You get the current year as string with
CStr(Year(Now))
Replace "REQ" with it.

How to transfer data from one table to another in Access VBA?

I thought this would be easy....that will be my famous last words!
I have an access database that is used to keep track of company vehicles. I would like the form used by associates to retain more information that they do not have to see the information populated. I have created a form (fTripInformation) that will allow them to select the company vehicle used for a work trip from a combo box (CarID). Vehicles are numbered for convenience (1-10). What I would like to happen is the associate selects the vehicle number from the combo box drop down menu. When that happens the corresponding information associated with the vehicle (Make,Model, Year, Color, etc...) is added to the temp table (tTemp) for this form from the vehicles table (tVehicles). This way the associate only see the vehicle #, yet all the other information will be recorded when the temp table information is transferred to the actual record table.
Any suggestions on how to do this?
This what I did:First, I redefined the Row Source Query the Combobox uses to include all the related information and updated the total Column Count in the Properties Sheet. Then, added the following code:
Private Sub CarID_Change()
Dim Rs As Recordset
Set Rs = CurrentDb.OpenRecordset("tTemp")
Rs.Edit
Rs!CarID = Me.CarID.Column(0)
Rs!Make = Me.CarID.Column(1)
Rs!Model = Me.CarID.Column(2)
Rs!Year = Me.CarID.Column(3)
Rs!Color = Me.CarID.Column(4)
Rs.Update
End Sub
Private Sub CarID_DblClick()
Dim Rs As Recordset
Set Rs = CurrentDb.OpenRecordset("tTemp")
Rs.Edit
Rs!CarID = ""
Rs!Make = ""
Rs!Model = ""
Rs!Year = ""
Rs!Color = ""
Rs.Update
End Sub
This is, in fact, pretty easy! Download the demo file from the link below and you will have the concept that you described above. Just modify the code to achieve your specific goals.
http://www.mediafire.com/file/x9cuorenr7r9mi5/AccessAndSQL4_2000.mdb/file

Adding a row after first row in a Table having only one row

I need to add a row after first row in MSWord Document. Document have already one row and two columns containing Country in first cell and Partner in second cell as values of both cells. Problem with the following code is it only work with at-least two rows.
Macro should be able to add row and write predefined strings in both columns. This can be done in two ways
By adding a row at the end of the table and reaching to it and writing the strings. If i do this then my question is how can I reach to the last row of the table?
By adding second row Everytime as mentioned in the following code.
Set doc = Documents.Open("C:\Users\dell\Desktop\LATAM.DOCX", , , , , , , , , , , True)
Set rng = doc.Content
rng.Tables(1).Rows.Add (rng.Tables(1).Rows(2)) 'Here I am getting error 'required memeber of collect not exist.
Set Cell = rng.Tables(1).Cell(2, 1)
Set Cell2 = rng.Tables(1).Cell(2, 2)
Cell.Range.Text = UT
If UT = "CROATIA" Then Cell2.Range.Text = "ERSTE SECURITIES ZAGREB"
If UT = "CZECH REPUBLIC" Then Cell2.Range.Text = "ERSTE GROUP"
Latam.docx look like this:
Requirement: After Macro it should be like this with values in added cells.
UPDATED ANSWER:
Set doc = Documents.Open("C:\Users\ibnea\Desktop\List of Countries & Companies CEEMEA & LATAM.DOCX", , , , , , , , , , , True)
Set rng = doc.Content.Tables(1).Rows.Add
rng.Range.Font.Bold = False
rng.Cells(1).Range = UT
rng.Cells(2).Range = UT2
Maybe I don't really understand your question but when I open a Word File and create a table with 1 row (as header) and 2 columns and enter "Country" and "Partner" then I can create a new row in this table with this code:
Option Explicit
Sub addRow()
Dim tbl As Table
'set tble variable to Table with Index 1 in your document
Set tbl = ActiveDocument.Tables(1)
'add a row to this table at the end
tbl.Rows.Add
'get last row in this table
Dim lastRow As Variant
lastRow = ActiveDocument.Tables(1).Rows.Count
'write to the last row
ActiveDocument.Tables(1).Cell(lastRow, 1).Range = "your value in last row / column 1"
ActiveDocument.Tables(1).Cell(lastRow, 2).Range = "your value in last row / column 2"
End Sub
You said your list has only one row. This code now adds a row and fills in column 1 and 2 the value whatever is on the right side of the equality sign. It's still not clear what UT is and where it comes from. But anyway you can now access the last cells with this code.
Use Rows.Add without a parameter and the new row will be inserted at the end of the table. The parameter (as stated in the Help topic for Rows.Add enables code to insert new rows before a specific row.
In order to work with the new row, declare an object variable and assign the row to it when it's created. It's also a good idea to do this with the Table object. That way it's possible to address the objects directly, rather than needing to always use something like rng.Tables(index).Rows(index) not only is this simpler to read and write, it also executes more quickly.
So, based on the code in the question my recommendation:
Dim tbl as Word.Table, rw as Word.Row
Set tbl = rng.Tables(1)
Set rw = tbl.Rows.Add
rw.Cells(1).Range.Text = "cell content"
rw.Cells(2).Range.Text = "other cell content"

Creating new columns for values that are repeating more than once so that all the column values will be converted to a row through EXCEL VBA

Im modifying the question that I have already posted. My requirement is very simple.
DEFECT_ID LOG_TIME STATUS
1001 08/22/2012 12:03:34 Open
1001 08/22/2012 12:03:35 Pending
1001 08/23/2012 02:13:46 Fixed
1001 08/23/2012 22:34:37 TestReady
1001 08/24/2012 12:34:43 Pending
1001 08/24/2012 19:13:39 Retest
1001 08/25/2012 22:13:40 Reopen
1001 08/26/2012 10:03:41 Retest
1001 08/27/2012 11:13:42 Closed
The above mentioned format is my 'Source' data. There will be 100s of such defects. As you can see, all the above log date and statuses belong to one single Defect_ID(1001).My actual work is I have to copy the above data into a new sheet in format that helps me to calculate the time difference between the statuses. To your attention, there are 'Eight' defect statuses: Open, Pending,Review,TestReady,Fixed,Retest,Reopen,Closed.And these defect statuses can occur more than one time in a single defect(As shown in above example, 'Pending' occurs twice. But openand closed will occur only once). Similarly there can be upto 6times a status can repeat for a Defect.So I need an output like this, where the log dates will be fitted into the corresponding statuses :
*Defect_ID* Open Pending1 Pending2...Pending6 Fixed1...Fixed6 TestReady1..Testready6 Review1..Review6 Retest1..Retest6 REopen1..REopen6 Closed
Please let me know how to post pictures. if so I can show you how exactly I need the output through VBA. Please refer my other question: 'Copying values from one column that matches a ID to a new sheet by creating new columns for eeach values through EXCEL VBA', all I need in that is, I need new columns added up for every status that is repeating. so that all my values will be into one single row.
Here is some VBA that will do what you are looking for. (If I understand the requirements correctly). It may be a bit verbose, but should be easy to maintain. There is probably a more eloquent way of doing this.
Private Sub CreateOutputSheet()
Dim iLoop, iStartRow, iEndRow As Integer
Dim iPendingCount As Integer
Dim iFixedCount As Integer
'Base Col Numbers
Const colPending As Integer = 3
Const colColFixed As Integer = 9
'.....
Dim sDefectIdCurrent As String
Dim sDefectIdPrevious As String
Dim iTargetRow As Integer
Dim sCurrentStatus As String
Dim dCurrentTime As Date
sDefectIdPrevious = Sheets("Soure").Cells(intstartRow, 1)
sDefectIdCurrent = Sheets("Soure").Cells(intstartRow, 1)
For iLoop = iStartRow To iEndRow
sDefectIdCurrent = Sheets("Soure").Cells(iLoop, 1)
'Check the current problem
If sDefectIdCurrent <> sDefectIdPrevious Then 'Reset the col counters
iPendingCount = 0
iFixedCount = 0
'....
End If
sCurrentStatus = Sheets("Soure").Cells(iLoop, 3)
dCurrentTime = Sheets("Soure").Cells(iLoop, 2)
Select Case sCurrentStatus
Case "Open"
Sheets("Target").Cells(iTargetRow, 2) = dCurrentTime
Case "Pending"
iPendingCount = iPendingCount + 1
Sheets("Target").Cells(colPending + iPendingCount, 1) = dCurrentTime
Case "Fixed"
iFixedCount = iFixedCount + 1
Sheets("Target").Cells(colColFixed + iFixedCount, 1) = dCurrentTime
'...
Case "Closed"
Sheets("Target").Cells(iTargetRow, colClosedNo) = dCurrentTime
End Select
sDefectIdCurrent = Sheets("Soure").Cells(intstartRow, 1)
Next
End Sub