I have an Excel form that contains a project name that I want to make sure is unique when compared to values in a SQL table. I want to do this without importing the SQL table into the worksheet. I was thinking of something like this:
Dim uniqueProjectName as Int
Dim suggstedProjectName as String
suggestedProjectName = WorkSheets("Form").Range("ProjectName")
If (Select Count(pName) from PROJECTS.dbo.Projects WHERE pName = suggestedProjectName) = 0
Then uniqueProjectName = 1
Else uniqueProjectName = 0
Can anyone help with the syntax?
Thanks everyone, I was able to use an ADODB connection and everything works as designed now....
Related
I'm trying to retrieve the values from an existing query and assign variable to it using VBA (Access 2016), to be able to use it in the further code.
And I'm totally stuck in that step, a very simple one, but I'm a newbie in VBA.
I have a query, which in SQL looks like that:
SELECT Max(CatalogConditions.DefConditionID) AS MaxDefConditionID, Max(ImplantCondition.InteractionID) AS MaxImplantCondition_InterationID, Max(Results_Epilepsy_BSL.ResultEpilepsyBSL_ID) AS MaxBSLresultsID, Max(Results_Epilepsy_Post.ResultEpilepsyPost_ID) AS MaxPOSTresultsID
FROM ((CatalogConditions INNER JOIN ImplantCondition ON CatalogConditions.DefConditionID = ImplantCondition.DefConditionID) INNER JOIN Results_Epilepsy_BSL ON ImplantCondition.InteractionID = Results_Epilepsy_BSL.InteractionID) INNER JOIN Results_Epilepsy_Post ON ImplantCondition.InteractionID = Results_Epilepsy_Post.InteractionID;
So, I'd like to assign all these Max... IDs to variables in my code, which I'm going to use later.
If anyone could help me, it would be great. Thx
You open a recordset, and then read the values from its fields.
Dim rst As DAO.Recordset
Dim DefConditionID As Long
Dim InteractionID As Long
' ...
Set rst = CurrentDb.OpenRecordset("myQuery")
' Read values into variables
DefConditionID = rst!MaxDefConditionID
InteractionID = rst!MaxImplantCondition_InterationID
' ...
rst.Close
Note that this reads the values from the first record of the query, so this assumes that the query only returns a single record.
To read one single value from a query or table, you can also use the DLookup function.
I have a problem that i can't solve even if i looked everywhere for a solution. I got an excel that have to be written from a table in my db, problem is that the headers in my db are named different than the headers in my excel file and I need to keep them like that. Another problem is that the index of the columns are also diffrent...It would be great if i could make a query for each column and write the result to the excel file by indicating the column's index, something like that:
Dim column1 As String = "SELECT [columnName] from [tableName] "
and then
Sheets("SheetName").Columns("ColumnName").value = column1
obviously it doesn't work it's just to let you know my idea...
Build your SQL query to match the order of the excel file and then fill in your result like
for (int i = 0; i < table.Columns.Count; i++)
{
Sheets("SheetName").Columns(i).value = table.Columns[i].ColumnName
}
and add another for-loop for the rows on the table.Rows to fill in the values
I have a question about using LINQ to access a Database and trying to make use of it's version of accessing the LIKE comparison operator.
I know that LINQ has .Contains(), .StartsWith(), and .EndsWith() as comparison methods. However I am wondering if there is a way to programatically imcorporate SQL Wildcards into a LINQ statement without explicitly using these query operators. Let me explain my situation.
I am writing a program that accesses a database, and part of the program is a search window which the user can use to help them find specific database data. I would like to try and incorporate SQL Wildcards into the textbox fields for these search pages.
For example if a user enters the input 17% I'd want the program to check for anything in that specific column that starts with a 17. The same is true with %17 and 17 where I'd want it to search for columns that end with, and contain the values.
Currently, this is the code I have for my search method:
Public Function Data_Search(sData As List(Of String), DB As CustomDataContext) As DataGridView
Dim gridData As New DataGridView
Dim query = From p In DB.Parts
Select p.Part_Number, p.Part_Description, p.Supplier.Supplier_Name
for i = 0 To sData.Count - 1
If Not sData(i).ToString() = "" Then
Select Case i
Case 0
Dim partNum As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Number.Contains(partNum))
Case 1
Dim description As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Description.Contains(description))
Case 2
Dim supp As String = sData(i).ToString()
query = query.Where(Function(x) x.Supplier_Name.Contains(supp))
End Select
End If
Next
gridData.DataSource = query.ToList()
Return gridData
End Function
So right now I am trying to see if there is a way for me to modify the code in a way that doesn't essentially involve me putting a substring search into each Case section to determine if I should be using StartsWith(), Contains(), or EndsWith.
Thanks for your time.
If you are using LINQ to SQL, and you are talking to Microsoft SQL Server, then you can use SQLMethods.Like to implement SQL LIKE:
query = query.Where(Function(x) SQLMethods.Like(x.Part_Number, partNum))
I thought I had a simple update statement:
Update LotDestination Set Quantity = Quantity - 2 where Lot = '9002ex'
Quantity is a field type of Number.
No matter what value is in Quantity, it always is 0 after running this query.
Is this an MS Access syntax issue, as I thought this was a pretty standard way to do things in other db platforms?
I created the following code and it works perfectly:
Public Sub UpdateQuantity()
Dim SQL As String
SQL = "UPDATE LotDestination SET LotDestination.Quantity = LotDestination.Quantity-2 WHERE LotDestination.Lot='9002ex'"
DoCmd.RunSQL SQL
End Sub
I'm writing some code behind some spreadsheets and I need to loop through some code, like getting data from a database doing some formulas and moving the data to a new sheet. My code for getting the data from the database is getting all of the values in multiple columns where the data has not been reported and has the same file name ( the data is coming from a file ), I have a field which states whether or not it has be reported by a simple "Y" or "N", and a field which holds the filename it came from.
So I need a while that, "WHILE" there's data that hasn't been reported do the rest of my code ( this includes my first SQL statement I said as this get data that hasn't been reported from each individual filename ).
I've been trying to get this to work for days but just have not been able to figure out how, so any help would be very grateful.
Update:
Database has a entity called datareported, can either be "N" or "Y", and also datadatfile which is the name from which the data came from.
So,
WHILE datareported = 'N' THEN
"SELECT (the data rows I want)
FROM tbldata
WHERE datareported='N' and datadatfile =
(SELECT min(datadatfile)
FROM tbldata WHERE datareported='N')"
This means that I loop through the rest of my code WHILE there is data that hasn't been reported and only bring in data of the same name ( from datadatfile ) so that the code can be run on that data.
That's basically what I want to do and that's pretty much what I have tried. I have tried a few other things and normally get a return of type mis-match.
Cheers,
Sam
More information needed for a solution but this may be of some help to push you in the right direction...
'ws1 = worksheet object...
bC = true
cnt1 = 2 ' starting row
Do While bC
If ws1.cells(cnt1, 1) = "N" Then
'Run your SQL here
Else
'Already reported...
End If
cnt1 = cnt1 + 1
If 'you hit the last row on your worksheet set bC = False to exit loop
Loop
Missed the update...this wont help, sorry.