When I run my code, the program cannot find the reference to my field. How can I fix this?
The field that has no reference is "Me![SetupMenuID]."
Dim con As Object
Dim rs As Object
Dim stSql As String
On Error GoTo SetupButtonClick_Err
' Find the item in the Switchboard Items table
' that corresponds to the button that was clicked.
Set con = Application.CurrentProject.Connection
Set rs = CreateObject("ADODB.Recordset")
stSql = "SELECT * FROM [Setup Sheet Items] "
stSql = stSql & "WHERE [SetupMenuID]=" & Me![SetupMenuID] & " AND [SetupItemNumber]=" & numBtn
rs.Open stSql, con, 1 ' 1 = adOpenKeyset
Related
I don't want to extract 0 values from Excel using the select query in VBA. I have used the below mentioned code for the same and it is working, like if there is 0 values present the code it is ignoring that but if there are no 0 values present in the Excel I am getting an error. So my main motive is if 0 values are present then ignore it using select query and if there are no 0 values in the Excel column then its okay just ignore the null values only.
Dim objConn As Object
Dim objRecordSet As Object
Dim objRecCmd As Object
Set objConn = CreateObject("ADODB.Connection")
Set objRecCmd = CreateObject("ADODB.Command")
Set objRecCmd_Update = CreateObject("ADODB.Command")
strFolderPath = "\inputexcel"
strQuery = "Select [BUNO],[RECHNR] from [Sheet1$] where [RECHNR] ='" & StrInvoiceNumber & "' AND ([AW_NUMMER] Is Not Null And ([AW_NUMMER] <> 0))"
objConn.Open ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFolderPath & ";Extended Properties=""Excel 12.0;IMEX=1""")
'Set objConn.Open = objConn.Execute(Source, Options:=Options)
Set objRecordSet = CreateObject("ADODB.Recordset")
objRecordSet.CursorLocation = adUseClient
objRecCmd.ActiveConnection = objConn
objRecCmd.CommandText = strQuery
objRecordSet.Open objRecCmd, , adOpenKeyset, adLockOptimistic
If Not objRecordSet.BOF And Not objRecordSet.EOF Then
objRecordSet.MoveFirst
End If
Using the test code at the end of this post...
Source table #1 (a null and a zero, no errors - results at right):
Source Table #2 (no zeros, and no errors - results at right)
Source Table #3 (no zeros, but cell C3 has an empty string value, so it's not really null). Gives error Datatype mismatch in query expression because of that one non-null non-numeric value in C3.
Sub TestSO()
Dim objConn As Object
Dim objRecordSet As Object
Dim objRecCmd As Object, strFolderPath, strQuery
Set objConn = CreateObject("ADODB.Connection")
Set objRecCmd = CreateObject("ADODB.Command")
strFolderPath = ThisWorkbook.Path & "\" & ThisWorkbook.Name
objConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFolderPath & _
";Extended Properties=""Excel 12.0;IMEX=1"""
strQuery = "Select [BUNO],[RECHNR],[AW_NUMMER] from [Sheet1$] where [RECHNR] ='" & "blah" & _
"' AND ([AW_NUMMER] Is Not Null And ([AW_NUMMER] <> 0))"
Set objRecordSet = CreateObject("ADODB.Recordset")
objRecordSet.CursorLocation = adUseClient
objRecCmd.ActiveConnection = objConn
objRecCmd.CommandText = strQuery
objRecordSet.Open objRecCmd, , adOpenKeyset, adLockOptimistic
[F2].CurrentRegion.ClearContents
If Not objRecordSet.BOF And Not objRecordSet.EOF Then
[F2].CopyFromRecordset objRecordSet '.MoveFirst
End If
End Sub
I have code that is pulling in data from a SQL table, and using VB and a data validation List Box that allows the user to select a criteria then it should pull in the data associated with that criteria: but I am not getting any errors and it is running the code but not pulling in any data. Can someone let me know what I am doing wrong: Here is my example: I select scenario from the Data Validation List Box and get " No Records Returned" here is my code:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim sSQL As String
Dim ceRg As Worksheet
Set ceRg = ThisWorkbook.Worksheets("RG Schedule")
If ceRg.FilterMode Then
ceRg.ShowAllData
End If
ceRg.Range("A3").ClearContents
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=SQL\SQLEXPRESS;Initial Catalog=Sample;Trusted_Connection=yes;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
sSQL = "SELECT [ORDER #],[SCENARIO],[LOCATION],[FORM],[STATUS],"
sSQL = sSQL & "CAST([Date Ordered] as date),"
sSQL = sSQL & "[Design],[Cost],"
sSQL = sSQL & "CAST([Critical Obligation Date] as date),"
sSQL = sSQL & "[MGMT_APPROVED],[AUTHORIZATION_Y_N],[CUSTOMER]"
sSQL = sSQL & "FROM [dbo].[vwRG_Schedule]"
sSQL = sSQL & "WHERE [SCENARIO] = '" & Scenario & "' "
sSQL = sSQL & "ORDER BY [ORDER #]"
Set rs = conn.Execute(sSQL)
' Check we have data.
If Not rs.EOF Then
' Transfer result.
ceRg.Range("A3").CopyFromRecordset rs
' Close the recordset
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
Sub LogCheck()
Dim cn As Object
Dim rs As Object
Dim StrSql As String
Dim strConnection As String
Dim AppPath As String
Set cn = CreateObject("ADODB.Connection")
AppPath = Application.ActiveWorkbook.Path
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\ceo.accdb;"
cn.Open strConnection
S_ID = Sheets("My").Range("A1").Value
StrSql = "SELECT * FROM EDO Where ID = ' " & S_ID & " '"
rs.Open StrSql, cn
If rs = Null Then
MsgBox "Record Not found"
Else
MsgBox "Record Found"
End If
End Sub
I am unable to run this code. Its showing error. Please help me out. Thanks!
Here S_ID is the data which I would like to search from table & ID is the primary key in the EDO Table.
In this case you may detect if the recordset is empty checking .EOF property:
Sub TestIfRecordFound()
Dim strConnection As String
Dim strID As String
Dim strQuery As String
Dim objConnection As Object
Dim objRecordSet As Object
strConnection = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source='C:\ceo.accdb';"
strID = Sheets("My").Range("A1").Value
strQuery = _
"SELECT * FROM EDO WHERE ID = '" & strID & "';"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open strConnection
Set objRecordSet = objConnection.Execute(strQuery)
If objRecordSet.EOF Then
MsgBox "Record Not found"
Else
MsgBox "Record Found"
End If
objConnection.Close
End Sub
If Id is numeric than the sql should be:
StrSql = "SELECT * FROM EDO WHERE Id = " & S_ID
You also did not define S_ID, so it will be handle as a variant here. If you still get an error, you might have to make it "& CStr(S_ID)".
Public Sub Ohno()
Dim stsql As String, results As String
Dim rs As Object, Db As Object, con As Object
Dim num As Integer
Dim start As Object
Set Db = CurrentDb()
Set con = Application.CurrentProject.Connection
Set rs = CreateObject("ADODB.Recordset")
num = 1
For num = 1 To 2
Do While IsEmpty(Forms("setup").Controls("TxtBoxEntry" & num)) = False
Set start = Forms("setup").Controls("TxtBoxEntry" & num)
stsql = "SELECT [Crosswalk].[Oracle GL Acct] FROM Crosswalk WHERE [Crosswalk].[Legacy GL Acct]= '" & start & "' "
rs.Open stsql, con
results = rs(0).Value
Forms("setup").Controls("TxtBoxRslt" & num).Value = results
Loop
Next
Set con = Nothing
Set rs = Nothing
I keep getting: Operation isn't allow while Object is Open - click me
the code does work for the first txtbox and stops to give me the above error. Am I setting up myself for failure on this one?
You need to close the recordset after using it. Try adding rs.Close:
Set start = Forms("setup").Controls("TxtBoxEntry" & num)
stsql = "SELECT [Crosswalk].[Oracle GL Acct]
FROM Crosswalk WHERE [Crosswalk].[Legacy GL Acct]= '" & start & "' "
rs.Open stsql, con
results = rs(0).Value
Forms("setup").Controls("TxtBoxRslt" & num).Value = results
rs.Close -- Add this here
Sub uoload_data()
Dim s(40) As Integer
Dim Row As Integer
Dim i As Integer
i = 0
For Row = 7 To 39
s(i) = Sheets("Data").Cells(Row, 5).Value
i = i + 1
Next
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Dim AppPath As String
Set cn = CreateObject("ADODB.Connection")
AppPath = Application.ActiveWorkbook.Path
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Users\devi\Desktop\Ability.accdb;"
cn.Open strConnection
strSql = "INSERT INTO MyTable Values ('" & s(0) & " ',
'" & s(1) & " ','" & s(2) & " ','" & s(3) & " ' )"
Set rs = cn.Execute(strSql)
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
I have a excel sheet of 40 field. I would like to update all field to access database. while insert record into database i am using insert into statement. In the mean time i need to write all fields of array into insert into statement. So please help me out to compact statement.
You can use Join() here
strSql = "INSERT INTO MyTable Values ('" & Join(s, "','") & "')"
The values in s() are integers, but you're wrapping the values in single-quotes, so are your DB columns text-type?
If they are numeric columns then you should drop the single-quotes.