Getting single value (User Id) from vba for ms access - vba

I'm just wondering and I'm not very familiar with MSAccess but I need to get the Username with the minimum number on a certain column. If there were more than one, I just need the first one. This is simple in other sql engines but Access doesn't allow me to add a min function to the where clause.
Sql = "SELECT top 1 IdUser from Sellers where Assignedleads = min(Assignedleads)"
Thanks

Related

sql query expression

The table retrieves data from the table "Group" with 2 columns First key "GroupNumber" and GroupName"I am trying to create a sql query in Microsoft Access from a table with two columns that will works as below:
Based on the user's selection from the first column the query has to return the value of the second column, and based on the user's selection from the second column the query has to return the value of the first.
Any idea how is possible to express the user's selection in sql? Ty
Some more data would be useful. AT this moment, it is hard to understand what it is exactly you are trying to do.
However, you mention two different selecting actions that need to be done by the user, so two SELECT statements would work.
To return the value of the second column based on user selection of the first: SELECT second_column_value FROM table WHERE first_column_value = value_selected_by_user
To return the value of the first column based on user selection of the second:SELECT first_column_value FROM table WHERE second_column_value = value_selected_by_user
or you can use drop-down lists, if the situation allows
Try providing more data to get more useful answers.

Need help modifying a large number of specific entries at once

My company has a large Access database which lists every user that has ever existed for a particular client of ours. This database is curated manually. I have been asked to delete 500+ users. This means I have to modify three columns for each user:
Status (must be changed to "deleted")
Date Deleted (to current date)
Date Revised (to current date)
Obviously, I don't want to have to Ctrl+F and change these fields manually for over 500 users. What is the easiest way to go about doing this more quickly?
I have the list of users that need to be deleted in Excel. I tried to create a query that shows all of these users in one table so that I don't have to sort through the users who don't need to be modified. It looked something like this:
SELECT UserID, Status, Date Deleted, Date Revised
FROM [database name]
WHERE UserID = 'a'
OR UserID 'b'
//(and then 500+ more OR statements for each UserID)
ORDER BY UserID;
I figured if I can at least do this, at least I'll have all the users I need to edit in front of me so that I don't have to Ctrl+F. But this didn't work, because it exceeded the 1,024 character limit in Access. Any ideas for how I can accomplish this?
Don't attempt to write 500+ UserID values into your SQL statement. Instead, import the Excel list as a table into your Access database. Then you can use that list of UserID values to identify which rows of your main table should be updated.
UPDATE MainTable AS m
SET m.Status = 'deleted', m.[Date Deleted] = Date(), m.[Date Revised] = Date()
WHERE m.UserID IN (SELECT UserID FROM tblFromExcel)

How to run sql query and loop through a column then pass that column data into another query from a second database

I am fairly new to SQL. I am using SQL Server 2014. I want to run a query on a database which returns a column of ID's. I am wondering if it is possible to loop over the column of ID's from the first database and pass them into another database to collect additional info.
Attempted to Google the answer but I'm not able to find a helpful scenario that mimics what I am looking for.
SELECT *
FROM dbo.MYDB1
WHERE CreatedLoc = 123
The above example spits out data but I only care about the ID column
I than want to loop over the ID column and for each run them on another database.
SELECT *
FROM dbo.MYDB2
WHERE ID IN (array of ids here, not hardcoded but dynamic)
Assuming appropriate permissions, you can access a different database than the one you're currently connected to using a fully qualified databasename.schemaname.tablename (or view, etc.)
If your databases are MyDB1 and MyDB2, you can run a query that looks something like this:
SELECT * from MyDB2.dbo.Table2
where ID IN (
SELECT ID from MyDB1.dbo.Table1 where CreatedLoc = 123
)

FILEMAKER SQL - Fetch column2 from records with distinct column1

How do I find a distinct set of records based upon one field while returning a different field from those records?
ExecuteSQL ( "SELECT DISTINCT account FROM Albums2"; ""; "" )
How do I get another field returned in this type of query?
My goal is fetch 1 record from 1 instance of each account in the list. I am going to populate a global with the id's from the records so that the accounts show in a portal. Then I want to select an account so that all records from the account will show in another portal.
Dan's answer below works perfectly:
SELECT account, MAX(id) FROM Albums2 GROUP BY account
I apologize for syntax error in my original answer. I no longer have Filemaker, but this code below will work with SQL. Does Filemaker 12 allow the SQL "GROUP BY" syntax? If so, it would solve the problem like this:
SELECT account, MAX(id) FROM Albums2 GROUP BY account
Explanation (assuming it works in Filemaker 12):
Rather than using DISTINCT to combine rows, you can use the GROUP BY syntax instead. To do that, you will also need to use an aggregate function like MAX() for the id column to indicate which of the non-duplicated id values to retain in the combined result.

MS Access alternative to SQL function ROW_NUMBER()

I work with MS Access 07 and need a little help.
I have two tables TbProjectTeam and TbProjectList. I need to compare date of employee turn-out and date of project start. I used that SQL syntax:
SELECT [TbProjectTeam ].[Surname, name]
FROM TbProjectTeam
INNER JOIN TbProjectList
ON TbProjectTeam .[DateofTurnOut] <= TbProjectList.[DateOfStart]
WHERE TbProjectList.[ID] = 1
ORDER BY [Surname, name];
My aim is to replace 1 in TbSeznamUkolu.[ID] = 1 expression with something as ROW_NUMBER() OVER in SQL. MS Access doesn't support this function but unfortunately I need to know row index of all projects. I imagine that will be displayed matching employees for every row.
Can anyone help me please? Big thanks.
MS Access has an AutoNumber data type that sets sequential numbers on records. Your data set would need to include an AutoNumber field to represent the ROW in T-SQL. Since the AutoNumber is numeric you could do > and < functions on it.
The only problem with this is that the records will be numbered sequentially as they are added to the table. If you can control the sequence in which they are added then there is no problem, but if you were to add the AutoNumber field to an existing populated table, the sequence may not match your requirements.
I realise this question is a bit old but I hope this helps.
DCOUNT function in spite of the efficiency issue provides a similar effect:
Just answered a similar answer here: How to show the record number in a MS Access report table?.