There is a table in MS Access having 3 columns:
ID (Primary key)
Date (Date/Time)
Train no (short text)
I have designed a form where date can be selected from a combo box and according to the date selected, respective trains should be displayed.
Problem is, the query works fine on dates above 10 (like 11/1/2015) but below 10 (like 9/1/2015) it gives error : "No current record" . The record is there in the table but it doesn't display.
The query is : SELECT DISTINCT [Train No] FROM Issue WHERE [Date] = #" & dt & "#"
dt is the date selected from the combo box.
Try this:
Dim DateSelected As Date
Dim DateString As String
DateSelected = DateValue(Me!YourComboBox.Value)
DateString = Format(DateSelected, "yyyy\/mm\/dd")
SELECT DISTINCT [Train No] FROM Issue WHERE [Date] = #" & DateString & "#"
Related
I am not sure if there is another post like this but i think my question is a little different.
I am currently designing a database to track employee training given by employer. My current error I find is adding an employee by using a bound form (frmAddEmployee) to my employees table (tblEmployees).
What I have at the moment that works is VBA code that shows a notification when you enter a value into a textbox for the employee number and it finds a duplicate record in the table. The VBA will also show the record in the same form that corresponds with the value you have entered when you clear the notification.
Here's the code I use for the txtEmpNumber after update:
Private Sub txtEmpNumber_AfterUpdate()
Dim EmpNum As String
Dim stLinkCriteria As String
Dim EmpNr As Integer
'Assign the entered employee number to a variable
EmpNum = Me.txtEmpNumber.Value
stLinkCriteria = "[EmpNumber] = " & "'" & EmpNum & "'"
If Me.txtEmpNumber = DLookup("[EmpNumber]", "tblEmployees", stLinkCriteria) Then
MsgBox "This employee number, " & EmpNum & ", has already been entered in database." _
& vbCr & vbCr & "Please check the number.", vbExclamation, "Duplicate information"
Me.Undo
'show the record of matched employee number from the employees table
EmpNr = DLookup("[EmpID]", "tblEmployees", stLinkCriteria)
Me.DataEntry = False
DoCmd.FindRecord EmpNum, , , , , acCurrent
Me.cmdSave.Enabled = False
Me.cmdNew.Enabled = True
Else
Me.txtIDNumber.Enabled = True
End If
End Sub
The notification shows there is a duplicate value, for example 1234 and shows the record in the table for 1234 but when I try a different value like 5678 then it shows the value again for 1234 and not 5678.
Any ideas how to fix this problem?
I think the problem you are having is that this command:
DoCmd.FindRecord EmpNum, , , , , acCurrent
...searches in the current field. But if there isn't a current field (because the form doesn't have the focus), or if the current field is some other field (such as ID), the "find" won't find a match, and the record you're looking at won't change.
The simplest fix would be to change your code to this:
DoCmd.FindRecord EmpNum, , , , , acAll
That will search for the value of EmpNum in ALL the fields.
But, if there's a chance that the value for EmpNum might also appear in other fields than txtEmpNumber (for example, if someone has EmpNum = 12 and someone else is ID = 12), this will eventually start "finding" the wrong record.
So I think the best thing to do would be to make sure your current field is txtEmpNumber before you execute the FindRecord.
Me.txtEmpNumber.SetFocus
DoCmd.FindRecord EmpNum, , , , , acCurrent
That will ensure it only looks in txtEmpNumber when it's trying to find the record.
I would like to save the result from the SELECT to a variable, in MS Access.
It should select the most recent offer price from the PriceFeed table where stock symbol in the table matches the selected item from the form comboBox.
Dim sq2 As Variant
sql = "SELECT PriceFeed.Offer FROM PriceFeed WHERE PriceFeed.StockSymbol = Me.CBSymbol.Column(1) AND DateTime =(SELECT MAX([PriceFeed.DateTime])FROM PriceFeed)"
DoCmd.RunSQL sq2
You can use DMax and DLookup:
RecentPrice = DLookup("Offer", "PriceFeed", "StockSymbol = " & Me.CBSymbol.Column(1) & " AND DateTime = DMax('DateTime', 'PriceFeed')")
To avoid errors if no CBSymbol has been selected:
RecentPrice = DLookup("Offer", "PriceFeed", "StockSymbol = " & Nz(Me.CBSymbol.Column(1), 0) & " AND DateTime = DMax('DateTime', 'PriceFeed')")
Using SQL Server Management Tools I created a view with the following
SELECT ID,
Groupname,
CONVERT(VARCHAR, TimeSub, 103) AS Date,
RIGHT(CONVERT(CHAR(20), TimeSub, 22), 11) AS Time,
TK,
CProblem,
Fix,
Username,
Closed_Date,
Reason
FROM dbo.log
I needed to seperate TimeSub into 2 fields 1 for Date and 1 for Time in AM/PM.
Using VB2012
Dim sqlSelectCommand As New SqlCommand(("Select * from log order by ID DESC"), CS)
Dim adapter As New SqlDataAdapter(sqlSelectCommand)
adapter.Fill(DTable)
BS.DataSource = DTable
DG1.DataSource = BS
BS.Filter = ("Date >= '" & Date.Now.ToString("dd/MM/yyyy") & "'")
'BS.Filter = ("Groupname = 'SW'")
What should happen is the datadridview should only show todays date based on the Date Column, it doesn't work, it shows all dates.
If I used the Groupname SW it sorts Groupname showing all records with SW in them. To me it's like the convert in the SQL view isn't working properly.
Any help appreciated.
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.
I have an Access Form - lets call it "Add Labor" (Access 2007) that saves data into a table.
The table has two columns in particular called "Start Date" and "End Date" (This table stores tasks)
There is also another table called FiscalYears which includes Start and End Dates for Fiscal Years, which is structured as follows
FyID
FYear
StartDate
EndDate
Example Data:
FYId FYear StartDate EndDate
-----------------------------
1 2010 10/1/2009 9/30/2010
2 2011 10/1/2010 9/30/2011
So in My Add Labor Form if someone enters labor that span across two fiscal years I need to enter two labor entries. Here is an example
If a user selects Labor Start Date = 6/30/2009
And End Date 10/2/2010 , it spans two fiscal years
So in my Labor Table I should enter two things
LaborID StartDate EndDate
-----------------------------
1 6/30/2009 9/30/2010
2 10/1/2010 10/2/2010
Basically I need to do a check before I save the record and add two records if they span Fiscal years, right now I'm just blindly doing Save Record on the form (inbuilt), but I guess I need to add some VBA. I've hardly ever used Access so this may be simple(hopefully). I am thinking instead of the event which just calls Save Record, I need it to add custom VBA.
Say you have an unbound form for adding the dates, you can say:
Dim rsFY As DAO.Recordset
Dim rsAL As DAO.Recordset
Dim db As Database
Dim sSQL As String
Set db = CurrentDb
''Select all years from the fiscal years table
sSQL = "SELECT FYear, StartDate, EndDate " _
& "FROM FiscalYears WHERE StartDate>=#" & Format(Me.StartDate, "yyyy/mm/dd") _
& "# Or EndDate <=#" & Format(Me.Enddate, "yyyy/mm/dd") _
& "# ORDER BY FYear"
Set rsFY = db.OpenRecordset(sSQL)
Set rsAL = db.OpenRecordset("AddLabor") ''table
''Populate recordset
rsFY.MoveLast
rsFY.MoveFirst
Do While Not rsFY.EOF
''Add records for each year selected
rsAL.AddNew
If rsFY.AbsolutePosition = 0 Then
rsAL!StartDate = Format(Me.StartDate, "yyyy/mm/dd")
Else
rsAL!StartDate = rsFY!StartDate
End If
If rsFY.AbsolutePosition + 1 = rsFY.RecordCount Then
rsAL!Enddate = Format(Me.Enddate, "yyyy/mm/dd")
Else
rsAL!Enddate = rsFY!Enddate
End If
rsAL.Update
rsFY.MoveNext
Loop
If the code was running in a main form with a subform showing the Addlabor table, you could update the subform to show the new records like so:
Me.Addlabor_subform.Requery
Why do you need a FiscalYears table? If your organization's fiscal years always start on Oct. 1 and end on Sept. 30, you can use a function to determine the fiscal year for a given date.
Public Function Fy(ByVal pDate As Date) As Integer
Dim intYear As Integer
Dim intReturn As Integer
intYear = Year(pDate)
If pDate > DateSerial(intYear, 9, 30) Then
intReturn = intYear + 1
Else
intReturn = intYear
End If
Fy = intReturn
End Function
And simple functions to return the Start and End dates for a given year.
Public Function FyStart(ByVal pYear As Integer) As Date
FyStart = DateSerial(pYear - 1, 10, 1)
End Function
Public Function FyEnd(ByVal pYear As Integer) As Date
FyEnd = DateSerial(pYear, 9, 30)
End Function
You can then determine how many fiscal years are included in a given date range by:
Fy(EndDate) - Fy(StartDate)
But I may be totally off base because you said "Start Date = 6/30/2009 And End Date 10/2/2010" spans two years. However, this expression returns 2 (3 years):
Fy(#10/2/2010#) - Fy(#6/30/2009#)