how to extract particular cell to my SQL in EXCEL - mysql

Here is a example table in Excel:
ID Name Account
1 Jon 001
2 Snow 002
3 Alive 003
I want to create a SQL according to account. It looks like,
SELECT * FROM CUSTOMER_TABLE WHERE ACCOUNT=' ';
Is it possible to make system could fulfill ACCOUNT=' '? Like system could input each value automatically.
It should like WHERE ACCOUNT='C1', ACCOUNT='C2', ACCOUNT='C3'...

I assume you copied your accounts into a range like Range("E1:E10").
Then you can use a for loop to change accounts for every turn.
Change your Query as,
SQL_Query = "Select * From Customer_Table Where Account ="
then try a For Loop.
Dim Query as String
For i = 1 to 10
Query = SQL_Query & Cells(i,4).value
'Do your query here.
'Be careful about that your results should be copied under the last row.
'Otherwise only the last one will be shown.
Next

Related

Create a Custom ID in Access

I would like to create a Custom ID in format, "SACYear-Sequential Number", in an Access 2013 Table (Table Name = poledata) when records are appended into that Table.
eg. SAC18-1 (18 means 2018)
I have researched and found the code as below. As advised, I have to add 2 new fields in the Table, alternate_id_seq and alternate_id. However, errors show up for the following lines.
Expression = "SAC" & Year(Date()) Mod 100 & "-"
Look Up A Record In
Select pd.[alternate_id_seq]
FROM poledata AS pd
ORDER BY pd.[alternate_id_seq] DESC;
What's going wrong with them?

Access VBA - getting value of current record

I'm a very new user of Access 2016 - I have a form populated by a query, and I want to create a hyperlink out of one of the text boxes. The link should include the combined values of the workday and taskID fields for the specific record the user clicks on.
My problem is that I can't figure out how to get the value of the current record. The control source for these columns are the values themselves, retrieved via the query populating the form's record source. The following code creates the link I want, but it returns the same workday and taskID values every time, regardless of what I click on, and I can't figure out how to make it respect the current record. Any help would be greatly appreciated!
Private Sub workday_Click()
Dim link As String
Dim workday as String
Dim taskID as string
workday = Format(Me!workday, "yyyy-mm-dd")
taskID = Me!taskID
link = "http://myurl.com/" & workday & "/" & taskID
Application.FollowHyperlink (link)
End Sub
EDIT - hope this helps explain a little better
The main table driving this has workID (PK), Workday, taskID, StatusID, DateLastUpdated, UpdatedByUserID. The query used for my form selects all from this table and joins in metadata tables for Task, Status and User. There are multiple records with the same workday value and the same taskID, but never the same combination of the two. The form has a few filters in the header, and the bottom section is a datasheet. In the datasheet I have a combo box used to update StatusID, all other fields are disabled and locked. My hyperlink click event is on the Workday field, and is working in that it creates the appropriate URL, it's just populated with the same taskID and workday values, eg even if I click on the row for "2016-07-12" "456", I still return "2016-07-11" & "123"
Here's a simplified version of what the data looks like. My goal is that when the user clicks on a workday, they are taken to a hyperlink made up of both the workday and TaskID of the row clicked on.
Workday TaskID
2016-07-11 123
2016-07-11 456
2016-07-11 789
2016-07-12 123
2016-07-12 456
2016-07-12 789
2016-07-13 123
2016-07-13 456
2016-07-13 789
I think I actually solved it - the problem was that since almost all fields were locked and disabled, the current record wasn't technically the row I was clicking on. I set Enabled = Yes for the Workday column, and now it's working as expected.

Ms Access 2003 VBA. Random order of table recordset?

I open a recordset (A) and use record value to process some data, and write a new recordset (B), with record value from recordset (A) and data value returned from processing.
E.g
RS A RS B
123 789 grape
456 456 apple
789 123 three
112 123 two
456 orange
123 one
112 blue
112 green
Now what I see is that, instead of reading data from recordset (A) in sequential manner it reads data in random order. So when there is large volume of data and macro hangs up in between, I have to spend some amount of time, to check which records were written in recordset (B), and which were not.
Why it is so, instead of reading records in sequential manner, it reads in random order.
My code looks like below:
Set objDb = Application.CurrentDb
Set RecA = objDb.OpenRecordset("select * from tblA")
' am I opening recordset in proper manner
' or I have to fill other parameters too?
Set RecB = objDb.OpenRecordset("select * from tblB")
While RecA.EOF <> True
RecA.MoveNext ' move to next record
RecB.AddNew ' add new
RecB.Update ' update
Wend
Regards.
It's not random, it just looks that way. If you run it multiple times, you'll see that it comes out in the same order.
If you want to select the order it comes out in, then use the ORDER BY clause in your SQL. So, for you, it would be:
SELECT * FROM tblA ORDER BY {fieldname}

Can SQL query do this?

I have a table "audit" with a "description" column, a "record_id" column and a "record_date" column. I want to select only those records where the description matches one of two possible strings (say, LIKE "NEW%" OR LIKE "ARCH%") where the record_id in each of those two matches each other. I then need to calculate the difference in days between the record_date of each other.
For instance, my table may contain:
id description record_id record_date
1 New Sub 1000 04/14/13
2 Mod 1000 04/14/13
3 Archived 1000 04/15/13
4 New Sub 1001 04/13/13
I would want to select only rows 1 and 3 and then calculate the number of days between 4/15 and 4/14 to determine how long it took to go from New to Archived for that record (1000). Both a New and an Archived entry must be present for any record for it to be counted (I don't care about ones that haven't been archived). Does this make sense and is it possible to calculate this in a SQL query? I don't know much beyond basic SQL.
I am using MySQL Workbench to do this.
The following is untested, but it should work asuming that any given record_id can only show up once with "New Sub" and "Archived"
select n.id as new_id
,a.id as archive_id
,record_id
,n.record_date as new_date
,a.record_date as archive_date
,DateDiff(a.record_date, n.record_date) as days_between
from audit n
join audit a using(record_id)
where n.description = 'New Sub'
and a.description = 'Archieved';
I changed from OR to AND, because I thought you wanted only the nr of days between records that was actually archived.
My test was in SQL Server so the syntax might need to be tweaked slightly for your (especially the DATEDIFF function) but you can select from the same table twice, one side grabbing the 'new' and one grabbing the 'archived' then linking them by record_id...
SELECT
newsub.id,
newsub.description,
newsub.record_date,
arc.id,
arc.description,
arc.record_date,
DATEDIFF(day, newsub.record_date, arc.record_date) AS DaysBetween
FROM
foo1 arc
, foo1 newsub
WHERE
(newsub.description LIKE 'NEW%')
AND
(arc.description LIKE 'ARC%')
AND
(newsub.record_id = arc.record_id)

Move data from field to field in MS Access

In MS Access I have a table like this:
TABLE USER
Id (int)
Status (text)
OldStatus (text)
I would like to create a MS Access Query which, when executed, moves the content of Status field to the top of OldStatus, and adds the current year. For example:
Before execution:
ID STATUS OLDSTATUS
1 Very good [2010] Excelent
[2009] Very bad
2 Excelent [2010] Bad
[2009] Good
After execution:
ID STATUS OLDSTATUS
1 [2011] Very good
[2010] Excelent
[2009] Very bad
2 [2011] Excelent
[2010] Bad
[2009] Good
How can I do that? THANK YOU!!
Didn't know the name of your table, so you'll have to substitute. The goal here is to maintain the old status while concatenating the current status. I used a space (" ") to put between, but you can use any character you want.
Update Status_Table
Set OLDSTATUS = OLDSTATUS & " " & STATUS
For what it is worth, there is a better way. Create a StatusHistory table:
StatusHistoryID
, StatusID
, Status
, Status_Date
You would have more flexibility here. Reports could be set for a date range or a particular year. You're table would be very hard to run a report on the status for 2010.