MS ACCESS Retrieving "Table Description" Through Query - ms-access

I've been looking everywhere for a way of accessing a table's description (same one that appears when you right click a table>table properties) through a SELECT query.
I tried using MSysObjects but I can only retrieve the name of the table using that.
Is it possible to do this through a query or is VBA needed?

As Remou says, you can't get it from a query (but you can include a function that returns it in a query). Here's another function:
Public Function GetTableDescr(stTableName As String) As String
On Error Resume Next
GetTableDescr = CurrentDb.TableDefs(stTableName).Properties("Description").Value
End Function
Here's a query that returns all the non-system tables, with their dates and descriptions (using the function above):
SELECT MSysObjects.Name, msysobjects.datecreate, msysobjects.dateupdate, GetTableDescr([Name]) AS Description
FROM MSysObjects
WHERE (((MSysObjects.Name) Not Like "~*") AND((MSysObjects.Name) Not Like "MSys*") and ((MSysObjects.Type)=1));
Finally, you can do an almost identical function for queries. The trick I found is that you only return non-inherited descriptions, otherwise if a query has no description you get the description of the queried object:
Public Function GetQueryDescr(stQryName As String) As String
On Error Resume Next
If CurrentDb.QueryDefs(stQryName).Properties("Description").Inherited = False Then
GetQueryDescr = CurrentDb.QueryDefs(stQryName).Properties("Description").Value
End If
End Function
The On Error Resume Next is necessary, because until the object has a description the property is null.

You can get the description from the table schema or from TableDef properties, but I do not think a standard query will work.
Set rs = CurrentProject.Connection.OpenSchema(adSchemaTables, _
Array(Empty, Empty, "Rules", Empty))
Debug.Print rs!Description

Using GetQueryDescr() above, you can run this query against the hidden sys table
SELECT MSysObjects.Name, GetQueryDescr([Name]) AS Properties, MSysObjects.DateCreate, MSysObjects.DateUpdate
FROM MSysObjects
WHERE (((MSysObjects.Name) Not Like "~sq_*") AND ((MSysObjects.Type)=5));
type 5 is for queries

Related

Why am I getting error 3078 on my SELECT query?

I've started to try and write some VBA to execute some queries and I've got stuck at the first hurdle. This is giving an error 3078 which apparently means it can't find the table or query. The table definitely exists and is spelt properly. Indeed the SQL runs fine - I tested it. What am I doing wrong?
Public Function Tester()
str_tbl = "tblGames_atp"
str_mkvrec = "SELECT * FROM " & str_tbl
dbl_fs_pct = DSum("FS", str_mkvrec)
End Function
Cannot reference SQL statement in domain aggregate function, not even a variable set to that statement. Must reference table or query object name. Could reference variable with name string but variable not really needed in this code. If you want function to return a value to calling source, then need to set function value.
Public Function Tester()
Tester = DSum("FS", "tblGames_atp")
End Function

OnlyDigits Function in Criteria

My database has a job number field that consists of year+month/serialnumber+type. There can be multiple jobs with the same job number:
201812/6Door
201812/6Stair
201812/6Wardrobe
When the user wants to change the date of any/all of these records I want all 201812/6 jobs to show in a form.
I have successfully used the OnlyDigits function below to pull only numbers from the text field: OnlyDigits(JobNumber) = 2018126. But I can't figure out how to filter the form to show all jobs containing 2018126.
I have tried using this query but get an error saying expression typed incorrectly or is too complex.
SELECT onlydigits(jobnumber) AS JobNumberDigits, tbldelivery.DelDateDoors, tbldelivery.Lag, tbldelivery.ProductionDate, tbldelivery.OrderNumber, tbldelivery.JobNumber
FROM tbldelivery
WHERE (((onlydigits(jobnumber))=OnlyDigits([Forms]![tblDelivery]![JobNumber])));
I also tried using a where expression in Docmd.OpenForm but that didn't work either. Can anyone suggest how I use this function to filter?
Public Function OnlyDigits(ByVal pInput As String) As String
Static objRegExp As Object
If objRegExp Is Nothing Then
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Global = True
.Pattern = "[^\d]"
End With
End If
OnlyDigits = objRegExp.Replace(pInput, vbNullString)
End Function
Try specifying the parameter to free Access from guessing:
PARAMETERS
[Forms]![tblDelivery]![JobNumber] Text;
SELECT
OnlyDigits(JobNumber) AS JobNumberDigits,
tbldelivery.DelDateDoors,
tbldelivery.Lag,
tbldelivery.ProductionDate,
tbldelivery.OrderNumber,
tbldelivery.JobNumber
FROM
tbldelivery
WHERE
OnlyDigits(JobNumber)=OnlyDigits([Forms]![tblDelivery]![JobNumber]);
or:
WHERE
OnlyDigits(CStr(Nz(JobNumber, 0)))=OnlyDigits([Forms]![tblDelivery]![JobNumber]);

Convert values from Access Combo Box to string

I need to convert the values from combo box to a string so I can add that string to a variable, to a function to eventually add to a database.
Here is my sub that grabs text from my form and from the combo box:
Private Sub cbRowStudentGrade_Change()
Course_ID.SetFocus
rowCourseID = Course_ID.Text
StuRed_ID.SetFocus
rowStudentRedID = StuRed_ID.Text
cbRowStudentGrade.SetFocus
cbRowStudentGrade = cbRowStudentGrade.Column(0)
CurrentDb.Execute "qryInputGrades"
MsgBox (rowCourseID)
MsgBox (rowStudentRedID)
MsgBox (cbRowStudentGrade)
Requery
Repaint
End Sub
And here are the functions that I am using as criteria in my Access query builder.
Public Function funcRowCourseID() As String
funcRowCourseID = rowCourseID
End Function
Public Function funcRowStudentRedID() As String
funcRowStudentRedID = rowStudentRedID
End Function
Public Function funcCbRowStudentGrade() As String
funcCbRowStudentGrade = cbRowStudentGrade
End Function
My query:
INSERT INTO tblRegistrationGrade ( Red_ID, Course_ID, Grade )
VALUES (funcRowStudentRedID(), funcRowCourseID(), funcCbRowStudentGrade());
I think there is a datatype mismatch between the database and what the combobox value actually is. But, if there were a datatype mismatch wouldn't there be an error stating as such? My database requires short text, which these are.
You're doing this way too complicated.
You can directly refer from your INSERT query to the form controls:
INSERT INTO tblRegistrationGrade ( Red_ID, Course_ID, Grade )
VALUES (Forms!myForm!StuRed_ID, Forms!myForm!Course_ID, Forms!myForm!cbRowStudentGrade);
and get rid of almost all of the code, except calling the query.
As John wrote, it would be better placed in cbRowStudentGrade_AfterUpdate().
Or, instead of the INSERT query, use a DAO.Recordset with AddNew, see:
How to: Add a Record to a DAO Recordset
There you can also directly use the values from your from controls without worrying about data types.

filter dropdown in sqlform,'Query' object is not callable

I want to insert a form include two fields:coursename,teacher_id(reference auth_user),but the teacher_id field show all the id in auth_user,I only want the user's id whoes role=teacher to show in the dropdown.as all the users are in the same table(auth_user)
this is what i have done:but it show the error:'Query' object is not callable.
query=(db.auth_membership.user_id==db.auth_user.id)(db.auth_membership.group_id==db.auth_group.id)(db.auth_group.role=='teacher')
db.courses.teacher_id.requires=IS_IN_DB(db(query),'auth_user.id')
form=SQLFORM(db.courses)
return dict(form=form)
Thanks
jian
query=(db.auth_membership.user_id==db.auth_user.id)(db.auth_membership.group_id==db.auth_group.id)(db.auth_group.role=='teacher')
The above is not valid syntax. A Set object is callable, and when called, it returns another Set object, so you can chain Sets:
myset = db(query1)(query2)(query3)
If you instead want to work with Query objects, you have to make explicit conjunctions using the "&" operator:
myquery = query1 & query2 & query3

Display Parameter(Multi-value) in Report

Can anyone tell me how to display all the selected value of my multi value parameter in SSRS report. When giving parameter.value option it gives error.
You can use the "Join" function to create a single string out of the array of labels, like this:
=Join(Parameters!Product.Label, ",")
=Join(Parameters!Product.Label, vbcrfl) for new line
I didn't know about the join function - Nice! I had written a function that I placed in the code section (report properties->code tab:
Public Function ShowParmValues(ByVal parm as Parameter) as string
Dim s as String
For i as integer = 0 to parm.Count-1
s &= CStr(parm.value(i)) & IIF( i < parm.Count-1, ", ","")
Next
Return s
End Function
Hopefully someone else finds this useful:
Using the Join is the best way to use a multi-value parameter. But what if you want to have an efficient 'Select All'? If there are 100s+ then the query will be very inefficient.
To solve this instead of using a SQL Query as is, change it to using an expression (click the Fx button top right) then build your query something like this (speech marks are necessary):
= "Select * from tProducts Where 1 = 1 "
IIF(Parameters!ProductID.Value(0)=-1,Nothing," And ProductID In (" & Join(Parameters!ProductID.Value,"','") & ")")
In your Parameter do the following:
SELECT -1 As ProductID, 'All' as ProductName Union All
Select
tProducts.ProductID,tProducts.ProductName
FROM
tProducts
By building the query as an expression means you can make the SQL Statement more efficient but also handle the difficulty SQL Server has with handling values in an 'In' statement.