execute once a day per user until expire - mysql

I have a system with a subscription method. A user will get notified that his subscription is about to run out 3 days in advance.
The message works, but every time a user presses OK, and browse, a new sql insert will be sent.
What I want is that the user gets the message only ONCE a day.
Dim themsg = "Your subscription is about to end bla bla"
Set almost = ObjConn.execute("Select * from users where expire <= '"& DateAdd("d", -3, Date()) &"'")
If almost.eof then
Response.write "No users about to finish"
Else
dim sql
sql = "insert into events(uid, event, msg, sended, need_confirm, eventtime)"
sql = sql & " VALUES "
sql = sql & "('"& almost("id") &"', "
sql = sql & "'send_msg', "
sql = sql & "'"& themsg &"', "
sql = sql & "'1', "
sql = sql & "'1', "
sql = sql & "'"& Now() &"')"
on error resume next
ObjConn.execute sql,recaffected
End if
So as you can see everytime you enter this page, a new insert will be sent. Can I restrict it so it only send to this uid/user only ONCE a day, until the expiry date?

Add the what ever your unique identifier from your users table to the events table. That way you can do a quick SELECT from the events table for user and eventtime to determine if an INSERT should occur.

Thanks to #jradich1234 gave me idea on how to do it
added new column in events ´checkday´
Then added
Set kollar = ObjConn.execute ("Select * from events where uid='"& slut("id") &"' AND checkday='"& Date() &"'")
If kollar.eof Then
<<THE SQL INSERTION HERE >>
Else
Response.write "Already sent"
End if
So with this it check if the row with same ID & todays date found, if not insert.

Related

MS access update query issue with date range

I'm using this update query in MS access 2016
Update sampledata set VALUE= '" & Me.value & "' where Day between '" & Me.dayfrom & "' and '" & Me.dayto & "';
The strangest problem m facing is- It is considering the form values for start and end date but however updates records only for the start date. Example. If dayfrom is 01-Nov-2021 and dayto is 30-Nov-2021, the query is updating records of only 01-Nov-2021.
When I pass the day from as 30-Nov-2021, it is updating records for the whole month.
Note: This doesn't happen when I directly pass the values in the query, it happens only when i Pick data from FORM and apply it in query.
UPDATE table3
SET table3.status = "YES"
WHERE (((table3.transactiondate)>=[FORM]![Form3]![startdate] And
(table3.transactiondate)<=[FORM]![Form3]![enddate]));
When you run the query, two pop up input box will open, in the first one, enter the value for start date, e.g 01/01/2022 , in the second one enter the end date e.g 02/28/2022. Do check the date format in use by your system so you can be sure you are entering the date parameter in the right format.
As is, the date values will be casted to strings using your Windows settings.
So, force a format on the string expressions to have true string expressions for the date values:
Sql = "Update sampledata " & _
"Set VALUE = '" & Me!value.Value & "' " & _
"Where Day Between #" & Format(Me!dayfrom.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!dayto.Value, "yyyy\/mm\/dd") & "#;"

How to retrieve value from database and display in textbox

I am using vb6, and the database is mysql. There is this table called "absen", it has a field called "tglabsen" which storing dates in this format : dd/mm/yyyy. I tried to find records according to the date.
eg. find records who have dates between 01/01/2017 to 01/02/2017
My question is how to store the number in a variable, and then display it in a textbox? What I tried so far, I tested this :
number = "Select count(*) from absen where tglabsen >='" & DTPicker1 & "' and tglabsen <='" & DTPicker2 & "'"
KON.Execute number
txtjumlahabsen = number
But the textbox (txtjumlahabsen) is just showing the sql query above.
KON.Execute just executes the sql statement you stored in your number variable. It doesn't update the variable with the data, which is why your textbox is showing the sql statement.
You need to open a recordset to retrieve the data:
Dim rs as New Recordset
Dim countVal as Integer
number = "Select count(*) from absen where tglabsen >='" & DTPicker1 & "' and tglabsen <='" & DTPicker2 & "'"
rs.Open number, KON, adOpenForwardOnly, adLockReadOnly
If Not rs.EOF then
countVal = rs(0).Value
End If
rs.Close
txtjumlahabsen.Text = countVal
If your sql statement is successful, the value from count(*) will be applied to the countVal variable. If it is not successful, countVal will remain at zero.

How to avoid a "You must enter a value" error message in Access VBA

I'm having issues avoiding a "You must enter a value in the __ field" error message in Access 2016. I have three tables, Tasks, Users, and TaskAssignments, and a split form that looks like:
User Task Assigned?
User1 Task1 True
User1 Task2 False
User1 Task3 True
User2 Task1 False
User2 Task2 False
User2 Task3 True
User3 Task1 True
User3 Task2 True
User3 Task3 True
Each task can have multiple users assigned to it, and each user is assigned to multiple tasks. I want my form to display every possible value, then use a checkbox, so that I can click and add a user to that task. The TaskAssignments table has a primary key and a unique constraint on both TaskID and UserID.
The recordsource for my form is a query:
select x.UserName, x.TaskName, ta.is_assigned
from (select * from Tasks, Users) x
left join TaskAssignments ta on (ta.TaskID = x.TaskID and ta.UserID = x.UserID)
I have an on click event that checks if a record exists in TaskAssignments and either updates or inserts into TaskAssignments. When I debug.print and manually run my queries, they both do what's expected. When I manually insert a record into my TaskAssignments table, my form behaves how I expect. When I need to insert a new record, however, I receive a message stating that I must enter a TaskID in TaskAssignments.
I've tried requerying the form, but I still receive the error message. Why can't it find the record that I just inserted?
Help please?!? Do I need to drastically rethink my approach here?
Here's the VBA:
Private Sub is_assigned_Click()
Dim CurrentUser, AssignmentQuery As String, SelectedUserID, SelectedTaskID As Integer
Dim ShouldInsert, IsAssigned As Boolean
CurrentUser = Environ$("Username")
SelectedUserID = Me.UserID
SelectedTaskID = Me.TaskID
IsAssigned = Me.is_assigned
Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
Set db = CurrentDb
strSQL = "select UserID, taskID from TaskAssignments where UserID=" & SelectedUserID & " and taskID =" & SelectedTaskID & ";"
Set rs = db.OpenRecordset(strSQL)
If rs.EOF = True Then
ShouldInsert = True
Else: ShouldInsert = False
End If
If ShouldInsert = True Then
AssignmentQuery = "insert into TaskAssignments (UserID, taskID, DateAssignmentUpdated, AssignmentUpdatedBy, is_assigned) values " _
& vbCrLf & "(" & SelectedUserID & "," & SelectedTaskID & ",#" & Now & "#,'" & CurrentUser & "'," & IsAssigned & ");"
ElseIf ShouldInsert = False Then
AssignmentQuery = "update TaskAssignments set UserID=" & SelectedUserID & ", DateAssignmentUpdated=#" & Now & "#, AssignmentUpdatedBy='" & CurrentUser & "',is_assigned=" & IsAssigned _
& vbCrLf & " where taskID = " & SelectedTaskID & " And UserID = " & SelectedUserID & ";"
End If
MsgBox AssignmentQuery
db.Execute (AssignmentQuery)
Forms("Task Assignments").Requery
Set rs = Nothing
Set db = Nothing
End Sub
Edit - here are the queries produced:
Insert
insert into TaskAssignments
(UserID, TaskID, DateAssignmentUpdated, AssignmentUpdatedBy, is_assigned)
values (301,4,Now(),'mylogin',True);
Update
update TaskAssignments
set UserID=270, DateAssignmentUpdated=Now(), AssignmentUpdatedBy='mylogin', is_assigned=False
where TaskID = 1 And UserID = 270;
And a constraint on my TaskAssignments table. Both TaskID and UserID are set as required in my table design (which was my whole goal - I was hoping to avoid adding records to TaskAssignments until the user has actually been assigned to a task).
alter table TaskAssignments add constraint TaskAssignmentsConstraint unique (TaskID, UserID);
Beware of wrong datatypes, each Dim needs its own datatype!
Dim CurrentUser As String, AssignmentQuery As String
Dim SelectedUserID As Long, SelectedTaskID As Long ' don't use 16-bit Integer for ID columns
Dim ShouldInsert As Boolean, IsAssigned As Boolean
To avoid troubles with date/time formatting: the database engine knows Now(), so you can directly use this in the Insert SQL:
AssignmentQuery = "insert into TaskAssignments (UserID, taskID, DateAssignmentUpdated, AssignmentUpdatedBy, is_assigned) values " _
& vbCrLf & "(" & SelectedUserID & "," & SelectedTaskID & ", Now(), '" & CurrentUser & "'," & IsAssigned & ");"
If it still doesn't work, use Debug.Print AssignmentQuery instead of MsgBox and add the actual SQL to your question (Ctrl+G shows the output).
Edit
Re-reading the question and comment, I think the problem is:
You are editing a bound form, and are updating/inserting in the same table the form is based on. That's where the Write conflict on Update comes from, the other error is probably because the bound form is trying to insert a record when you click is_assigned, but can't.
So yes, you need to rethink your approach, at least partially.
One solution is to insert the recordsource into a temp table, and base your form on that. Then the rest of the code will probably work.
It may be over-complicating things, though.
I got this problem trying to update a field that use to be a primary field in my table. When I altered what was considered the primary field I assume access would automatically stop enforcing is not null, but for some reason it didn't.
I fixed it by deleting the field, saving the table recreating the field and saving the table and the problem went away. Of course this wouldn't be an ideal solution if you have data in that table you don't want to lose so you might want to try backing it up first, before you give the solution a try, then reinserting the values.

Form - Data validation to stop duplicate bookings Access

I am having problems coming up with a solution to the following problem:
I have a booking form, but when the user adds details I want to check my database to see if the exact vehicle has been booked out for those dates already. If it has, an error message would appear.
Your vehicles should have ID Numbers. So, when a user tries to book a vehicle, you check the ID against the table of booked vehicles. Something like this (aircode) should work:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset ("SELECT CarID FROM tblBookedCars WHERE CarID = " & Me.cboCarsToBook.Selected & "")
If rec.EOF Then
'Your car isn't booked, so process your booking
Else
Msgbox "That car is already booked"
End If
Assuming that booked out cars have a start and end date, then a booking collision occurs when:
RequestStartDate <= EndDate
and
RequestEndDate >= StartDate
The above is thus a rather simply query, but if any collision occurs, the above will return records, and you simply don’t allow the booking. And this means you don’t have to maintain some huge “messy” table of cars and each date that the car is book. You ONLY need simple row attached to a given vehicle with the start and end date (and thus it easy to change dates - you only update the one booking reocrd.
Air code to build such a query will look much like this:
dim strWhere as string
dim dtRequeestStartDate as date
dim dtRequestEndDate as date
dim lngCarID as long
dtRequestStartDate = inputbox("Enter start Date")
dtRequestEndDate = inputbox("Enter end date")
lngCarID = inputbox("What car id")
strWhere="#" & format(dtRequestStartDate,"mm/­dd/yyyy") & "# <= EndDate" & _
" and #" & format(dtRequestEndDate,"mm/dd­/yyyy") & "# >= StartDate and CarID = " & lngCarID
if dcount("*","tableBooking",strW­here) > 0 then
msgbox "sorry, you cant book
...bla bla bla....
The above is just an example, and I am sure you would build a nice form that prompts the user for the booking dates. However, what is nice here is that the above simple query will return ANY collision.

Moving Dates to Access Table Fields - Incorrect Dates Printed

I'm having some issues with moving the CurrentDate and LastDayOfMonth to a table in Access for data processing
Dim CD As Date
Dim LDOM As Date
CD = DateSerial(Year(Date), Month(Date), Day(Date))
'Format(Now(), "mm-dd-yyyy")
LDOM = DateSerial(Year(Date), Month(Date) + 1, 0)
'Add Dates
CurrentDb.Execute "UPDATE tblProcess " & _
"SET tblProcess.[CurrentDate] = " & CD
CurrentDb.Execute "UPDATE tblProcess " & _
"SET tblProcess.[DueDate] = " & LDOM
Debug.Print CD
Debug.Print LDOM
Everytime I Debug.Print - either the formula or the variable - it ALWAYS comes out correct.
But what ends up on my table for both fields is "12/30/1899" Can anyone help?
Test simply:
CurrentDb.Execute "UPDATE tblProcess" _
& " SET tblProcess.[CurrentDate] = #" & Format(CD, "yyyy-mm-dd") & "#;"
Your original code uses SQL like this:
UPDATE tblProcess SET tblProcess.[CurrentDate] = 12/03/2013
that is BAD for Access DATETIME field.
Instead we need in final for Accesss SQL string:
UPDATE tblProcess SET tblProcess.[CurrentDate] = #2013-12-03 22:00:13#;
Please stop voting up for such a small contribution, I have not said the last word, for SQL Server, we must use:
UPDATE tblProcess SET CurrentDate = '2013-12-03T22:00:13';
Although Access and SQL Server are both Microsoft of Bill Gates.