R
rebelscum0000
Dear All,
I do not undestand why this code is not working, I Only want to Sort
Ascending
The Field MyKeyword from the Tbl MyKeywords_Tbl
--->My Code:
'Counts ALL Records FROM Tbl MyKeywords_Tbl
MyToReCo_Keywords_Tlb = DCount("*", "MyKeywords_Tbl")
MsgBox "My Total Records are: " & MyToReCo_Keywords_Tlb
'Order my Records From Tbl MyKeywords_Tbl
strSelectSQL = "SELECT MyKeywords_Tbl.MyKeyword " & _
"FROM MyKeywords_Tbl " & _
"ORDER BY MyKeywords_Tbl.MyKeyword;"
CurrentDb.Execute strSelectSQL, dbFailOnError
rst_Keyword.Close
Set rst_Keyword = Nothing
Set db_Keyword = Nothing
---->I Get a Run-time error 3065*
"Cannot execute a select query."
I Found out The execute method only works with Action Queries (Update,
Append and Make
Table)
So I added and changed to my code:
Dim db_Keyword As Database 'Current Dbs
Table
Dim rst_Keyword As DAO.Recordset 'DAO.Recordset
Table
Dim strSelectSQL As String
Set db_Keyword = CurrentDb
Set rst_Keyword = db_Keyword.OpenRecordset(strSelectSQL)
DoCmd.RunSQL strSelectSQL
I Get a Run-time error 2342
"A RunSQL action requires an argument consisting of an an SQL
statementet"
-----> I did change :
DoCmd.RunSQL strSelectSQL to DoCmd.OpenQuery strSelectSQL
and I get another
Run-time error 7874
Microsof acces can't find the object 'SELECT
MyKeywords_Tbl.MyKeyword FROM Mykeywords_tbl ORDER BY
MyKeywords_Tbl.Mykeyword;.'
->>>>Please help me what I am doing wrong???
->>>>>@Dim db_Keyword As Database" is wrong? it shoul be "Dim
db_Keyword As DAO.Database"?
->>>>>>Also what is the diference between CurrentDb.Execute
strSelectSQL, dbFailOnError &
DoCmd.RunSQL strSelectSQL?
--->This is my entire code, Maybe is something wrong:
Thanks in advance
Antonio Macias
Private Sub Keyword_AfterUpdate()
Dim db_Keyword As Database 'Current Dbs
Table
Dim rst_Keyword As DAO.Recordset 'DAO.Recordset
Table
Dim SQL3, sQL4, strSelectSQL As String 'Select Query Table
Dim Msg, Style, Title, Response, MySelection
Dim MyCuKeywordID As Variant 'My Current
Keyword ID
Dim MyReSeKeyword As Variant 'My Record
Search Keyword
Dim MyToReCo_Keywords_Tlb As Variant 'My Total
Records Keywords_Tbl
'Make sure Microsoft DAO 3.6 Library in included in the References
'Initialize Variables
Msg = "Are you sure " & "[ " & Keyword & " ]" & " Will be your
default search?"
msg1 = Keyword & " Is now your default search"
msg2 = "The Keyword: " & "[ " & Keyword & "]" & " Already Exists,
Please enter another Keyword"
Style = vbYesNoCancel + vbQuestion 'Define buttons.
Style1 = vbYesNo + vbInformation 'Define buttons.
Style2 = vbOKOnly + vbInformation 'Define buttons.
Title = "Setting Keyword" 'Define title.
Title1 = "Keyword Set" 'Define title.
Title2 = "Keyword Dulicated" 'Define title.
MyReSeKeyword = _
DLookup("[MyKeyword]", "MyKeywords_Tbl", "[MyKeyword] =
'" & Keyword & "'")
If IsNull(MyReSeKeyword) Then
Response = MsgBox(Msg, Style, Title)
If Response = 6 Then 'User chose Yes.
MySelection = "Yes" 'Perform some action.
Response1 = MsgBox(msg1, Style1, Title1)
If Response1 = 6 Then
'This SQL Statement UPDATE All the Field MyKeyword OF THE
'Tbl MainExclude_Tbl With a Control Source (Keyword)
'This is an Action querie (Update, Append, and Make Table)
'When is an UPDATE Query and the Control Source Comes from
a
'Form the sintaxis has to be '" & Contol Source &"' ended
with ; and closed
'With a quote.
SQL3 = _
"UPDATE MainExclude_Tbl SET MainExclude_Tbl.MyKeyword = '"
& Keyword & "'"
'Please read Write Conflict Form Subform.pdf
If Me.Dirty Then Me.Dirty = False
CurrentDb.Execute SQL3, dbFailOnError
'Searchs the First occurrence that contains the Control
Source (Keyword)
'in the Tbl (MainExclude_Tbl) and Returns its ID, to ensure
that
'the DLookup function returns a unique value:
MyCuKeywordID = _
DLookup("[ID]", "MainExclude_Tbl", "[MyKeyword] = '" &
Keyword & "'")
'This SQL Statement INSERT INTO the Tbl (MyKeywords_Tbl) the
Control Source
'(Keyword) FROM the Tbl MainExclude_Tbl, WHERE "ONLY" INSERT
FROM Tbl
'MainExclude_Tbl The Variable MyCuKeywordID:
sQL4 = _
"INSERT INTO MyKeywords_Tbl ( MyKeyword ) " & _
"SELECT MainExclude_Tbl.MyKeyword " & _
"FROM MainExclude_Tbl " & _
"WHERE (((MainExclude_Tbl.ID) = " & MyCuKeywordID & " ))"
CurrentDb.Execute sQL4, dbFailOnError
------------------------ My Problem Here
--------------------------------------------------
'Counts ALL Records FROM Tbl MyKeywords_Tbl
MyToReCo_Keywords_Tlb = DCount("*", "MyKeywords_Tbl")
MsgBox "My Total Records are: " & MyToReCo_Keywords_Tlb
'Order my Records From Tbl MyKeywords_Tbl
strSelectSQL = "SELECT MyKeywords_Tbl.MyKeyword " & _
"FROM MyKeywords_Tbl " & _
"ORDER BY MyKeywords_Tbl.MyKeyword;"
Set db_Keyword = CurrentDb
Set rst_Keyword = db_Keyword.OpenRecordset(strSelectSQL)
DoCmd.OpenQuery strSelectSQL
rst_Keyword.Close
Set rst_Keyword = Nothing
Set db_Keyword = Nothing
-------------------------------------------------------------------------------------------------------------------------------
ElseIf Response1 = 7 Then
Keyword = ""
MsgBox "User Cancel", vbInformation, "Canceled
default Search"
End If
ElseIf Response = 7 Then 'User chose No.
MySelection = "No" 'Perform some action.
Keyword = ""
ElseIf Response = 2 Then 'User chose Cancel.
MySelection = Cancel 'Perform some action.
Keyword = ""
MsgBox "User Cancel", vbInformation, "Canceled default
Search"
End If
Else
Response2 = MsgBox(msg2, Style2, Title2)
Keyword = ""
Keyword.SetFocus
End If
'3075 Please enter a valid Keyword
End Sub
I do not undestand why this code is not working, I Only want to Sort
Ascending
The Field MyKeyword from the Tbl MyKeywords_Tbl
--->My Code:
'Counts ALL Records FROM Tbl MyKeywords_Tbl
MyToReCo_Keywords_Tlb = DCount("*", "MyKeywords_Tbl")
MsgBox "My Total Records are: " & MyToReCo_Keywords_Tlb
'Order my Records From Tbl MyKeywords_Tbl
strSelectSQL = "SELECT MyKeywords_Tbl.MyKeyword " & _
"FROM MyKeywords_Tbl " & _
"ORDER BY MyKeywords_Tbl.MyKeyword;"
CurrentDb.Execute strSelectSQL, dbFailOnError
rst_Keyword.Close
Set rst_Keyword = Nothing
Set db_Keyword = Nothing
---->I Get a Run-time error 3065*
"Cannot execute a select query."
I Found out The execute method only works with Action Queries (Update,
Append and Make
Table)
So I added and changed to my code:
Dim db_Keyword As Database 'Current Dbs
Table
Dim rst_Keyword As DAO.Recordset 'DAO.Recordset
Table
Dim strSelectSQL As String
Set db_Keyword = CurrentDb
Set rst_Keyword = db_Keyword.OpenRecordset(strSelectSQL)
DoCmd.RunSQL strSelectSQL
I Get a Run-time error 2342
"A RunSQL action requires an argument consisting of an an SQL
statementet"
-----> I did change :
DoCmd.RunSQL strSelectSQL to DoCmd.OpenQuery strSelectSQL
and I get another
Run-time error 7874
Microsof acces can't find the object 'SELECT
MyKeywords_Tbl.MyKeyword FROM Mykeywords_tbl ORDER BY
MyKeywords_Tbl.Mykeyword;.'
->>>>Please help me what I am doing wrong???
->>>>>@Dim db_Keyword As Database" is wrong? it shoul be "Dim
db_Keyword As DAO.Database"?
->>>>>>Also what is the diference between CurrentDb.Execute
strSelectSQL, dbFailOnError &
DoCmd.RunSQL strSelectSQL?
--->This is my entire code, Maybe is something wrong:
Thanks in advance
Antonio Macias
Private Sub Keyword_AfterUpdate()
Dim db_Keyword As Database 'Current Dbs
Table
Dim rst_Keyword As DAO.Recordset 'DAO.Recordset
Table
Dim SQL3, sQL4, strSelectSQL As String 'Select Query Table
Dim Msg, Style, Title, Response, MySelection
Dim MyCuKeywordID As Variant 'My Current
Keyword ID
Dim MyReSeKeyword As Variant 'My Record
Search Keyword
Dim MyToReCo_Keywords_Tlb As Variant 'My Total
Records Keywords_Tbl
'Make sure Microsoft DAO 3.6 Library in included in the References
'Initialize Variables
Msg = "Are you sure " & "[ " & Keyword & " ]" & " Will be your
default search?"
msg1 = Keyword & " Is now your default search"
msg2 = "The Keyword: " & "[ " & Keyword & "]" & " Already Exists,
Please enter another Keyword"
Style = vbYesNoCancel + vbQuestion 'Define buttons.
Style1 = vbYesNo + vbInformation 'Define buttons.
Style2 = vbOKOnly + vbInformation 'Define buttons.
Title = "Setting Keyword" 'Define title.
Title1 = "Keyword Set" 'Define title.
Title2 = "Keyword Dulicated" 'Define title.
MyReSeKeyword = _
DLookup("[MyKeyword]", "MyKeywords_Tbl", "[MyKeyword] =
'" & Keyword & "'")
If IsNull(MyReSeKeyword) Then
Response = MsgBox(Msg, Style, Title)
If Response = 6 Then 'User chose Yes.
MySelection = "Yes" 'Perform some action.
Response1 = MsgBox(msg1, Style1, Title1)
If Response1 = 6 Then
'This SQL Statement UPDATE All the Field MyKeyword OF THE
'Tbl MainExclude_Tbl With a Control Source (Keyword)
'This is an Action querie (Update, Append, and Make Table)
'When is an UPDATE Query and the Control Source Comes from
a
'Form the sintaxis has to be '" & Contol Source &"' ended
with ; and closed
'With a quote.
SQL3 = _
"UPDATE MainExclude_Tbl SET MainExclude_Tbl.MyKeyword = '"
& Keyword & "'"
'Please read Write Conflict Form Subform.pdf
If Me.Dirty Then Me.Dirty = False
CurrentDb.Execute SQL3, dbFailOnError
'Searchs the First occurrence that contains the Control
Source (Keyword)
'in the Tbl (MainExclude_Tbl) and Returns its ID, to ensure
that
'the DLookup function returns a unique value:
MyCuKeywordID = _
DLookup("[ID]", "MainExclude_Tbl", "[MyKeyword] = '" &
Keyword & "'")
'This SQL Statement INSERT INTO the Tbl (MyKeywords_Tbl) the
Control Source
'(Keyword) FROM the Tbl MainExclude_Tbl, WHERE "ONLY" INSERT
FROM Tbl
'MainExclude_Tbl The Variable MyCuKeywordID:
sQL4 = _
"INSERT INTO MyKeywords_Tbl ( MyKeyword ) " & _
"SELECT MainExclude_Tbl.MyKeyword " & _
"FROM MainExclude_Tbl " & _
"WHERE (((MainExclude_Tbl.ID) = " & MyCuKeywordID & " ))"
CurrentDb.Execute sQL4, dbFailOnError
------------------------ My Problem Here
--------------------------------------------------
'Counts ALL Records FROM Tbl MyKeywords_Tbl
MyToReCo_Keywords_Tlb = DCount("*", "MyKeywords_Tbl")
MsgBox "My Total Records are: " & MyToReCo_Keywords_Tlb
'Order my Records From Tbl MyKeywords_Tbl
strSelectSQL = "SELECT MyKeywords_Tbl.MyKeyword " & _
"FROM MyKeywords_Tbl " & _
"ORDER BY MyKeywords_Tbl.MyKeyword;"
Set db_Keyword = CurrentDb
Set rst_Keyword = db_Keyword.OpenRecordset(strSelectSQL)
DoCmd.OpenQuery strSelectSQL
rst_Keyword.Close
Set rst_Keyword = Nothing
Set db_Keyword = Nothing
-------------------------------------------------------------------------------------------------------------------------------
ElseIf Response1 = 7 Then
Keyword = ""
MsgBox "User Cancel", vbInformation, "Canceled
default Search"
End If
ElseIf Response = 7 Then 'User chose No.
MySelection = "No" 'Perform some action.
Keyword = ""
ElseIf Response = 2 Then 'User chose Cancel.
MySelection = Cancel 'Perform some action.
Keyword = ""
MsgBox "User Cancel", vbInformation, "Canceled default
Search"
End If
Else
Response2 = MsgBox(msg2, Style2, Title2)
Keyword = ""
Keyword.SetFocus
End If
'3075 Please enter a valid Keyword
End Sub