Data Report and Grouping (Shape Append) - vb6

I hard-coded a data report's DataMember and Datafields (I'm not using a DataEnviroment, and don't know how either. I'd rather code it personally) but the more I check the code, I can't get to solve this thing.
The error message I'm getting is:
Error '8526' Can't find DataField 'Omisions.Date'
in this part of the code:
rpt.Refresh
Just before I do a rpt.Show at the end of the code. rpt is my current datareport.
Here's the relevant code:
sql = "Shape {exec Usp_HoraExtra_Rut 1} As Normal " _
& "Append ({exec Usp_HoraExtra_Rut 2} As Omisions " _
& "Relate Nit To Nit)"
rptrs.Open sql, db, adOpenStatic, adLockReadOnly
I'm using two stored procedures instead because of the large queries.
Then I add the DataMember:
.Sections("Section1").Controls.Item("t_fecha").DataMember = "Omisions"
and then the DataFields:
.Sections("Section1").Controls.Item("t_fecha").DataField = "date"
and then:
rpt.Refresh
rpt.Show
If anyone can give me a hand, it'd be greatly apreciated.
Edit: typo, cleaned up the code.

For anyone who might read this...
I solved this problem by switching from stored procs to views. For some reason SHAPE doesn't work with sotred procs and I refuse to code a big giant SQL query. I just created a view on my database and query'd it.
Also, the connnection string needs changing.
All in all, hard nut.

Related

Export validation rules from Microsoft Access mdb file

I have an MDB file which contains a number of tables and forms. Each field has a validation rule such as Is Null Or >=0 And <=255.
This access database is being converted into an online system using MySQL. Exporting all the data is easy using MDBTools (https://github.com/brianb/mdbtools).
However I can't find any way of exporting the validation rules. There are thousands of fields across over 100 tables so it's going to be important to export and import them rather than rewrite each one.
I don't really mind what format they're exported in, any sort of text format so I could do a regular expression or something will be fine.
However I haven't been able to find any information anywhere on exporting these validation rules.
Perhaps if it's not built into access by default then a VB script could be used to find the info and write it to a text file? I'm not really familiar with access or windows at all so if anyone could suggest if that was a possibility that would be great.
Using VBA allows you to retrieve field validation rules directly.
I realize it's probably too late to help you now. And, although it may not seem appropriate for someone unfamiliar with Access and VBA, this approach requires only a table, copying the code below into a standard module, and running it. So someone else may benefit.
I created my table, field_validation_rules, to store the text of the validation rule properties. The table includes 3 text fields: table_name; field_name; and validation_rule.
Public Sub GatherValidationRules()
Dim db As DAO.Database
Dim fld As DAO.Field
Dim rs As DAO.Recordset
Dim tdf As DAO.TableDef
Set db = CurrentDb
Set rs = db.OpenRecordset("field_validation_rules", dbOpenTable, dbAppendOnly)
For Each tdf In db.TableDefs
If Not (tdf.Name Like "~*" Or tdf.Name Like "MSys*") Then
For Each fld In tdf.Fields
If Len(fld.ValidationRule) > 0 Then
rs.AddNew
rs!table_name.Value = tdf.Name
rs!field_name.Value = fld.Name
rs!validation_rule.Value = fld.ValidationRule
rs.Update
End If
Next
End If
Next
rs.Close
End Sub
The ValidationRule property is a string value. If the property has not been assigned for a given field, ValidationRule is an empty string. The code skips those, storing only validation rules for fields which have them assigned.
If you want the collected validation rules in a text file, there a several options. I dumped mine to CSV like this:
DoCmd.TransferText acExportDelim, , "field_validation_rules", "C:\share\Access\field_validation_rules.txt", False
To anyone else finding this, this is how I wound up doing it. This was in Access 2003, it may be different in other versions.
First I went to Tools > Analyze > Documenter selected the table I wanted and used these settings:
I was then presented with what looked like a pdf or word doc (I don't think it is, but it doesn't really matter).
I then did File > Export and selected "Text Files .txt" and saved it to a location on my computer.
I then opened the .txt file in PHP (anywhere you can do regular expressions should be fine).
In my instance not every single field had validation rules and the validation rules did not appear if they were not set, which meant a regular expression to fetch the fieldID had more results than to fetch the validation rules.
So I used two regular expressions.
/SourceField:\s+(\S+).*?AllowZeroLength/msi
This gets everything betwenen SourceField and AllowZeroLength. AllowZeroLength is the first bit of repeating text after the validation rules.
I then used this regular expression to get the validation rules from within that string.
/ValidationRule:\s+(.*)\\r/
I had to use \r instead of new line, probably something to do with moving it from Windows to Ubuntu.
In PHP it looked like this:
<?php
$file_contents = file_get_contents('validations.txt');
$response = [];
preg_match_all('/SourceField:\s+(\S+).*?AllowZeroLength/msi', $file_contents, $matches);
for($i = 0; $i < count($matches[0]); $i++) {
$id = $matches[1][$i];
preg_match('/ValidationRule:\s+(.*)\\r/', $matches[0][$i], $validation_match);
$response[$id] = $validation_match[1] ?? null;
}
There is almost certainly a cleaner regular expression than this, but this was incredibly quick and I got exactly what I wanted.

VBScript in Classic ASP

I'm working with a very weird version of VB...it doesn't want me telling it what is what, it wants to figure that out on its own.
In C# I can easily hard code an array...not so much in this VB.
I would like to create a hard coded array while calling the function...but I'm not sure about the syntax. Can't find much on this specific VB version. It doesn't let you declare types. Anyone here know how to do this? If so, thanks!
FUNCTION HasInput(filters())
HasInput = False
FOR EACH table IN filters
FOR EACH key IN Request.Form
IF LEFT(key, LEN(table)) = table AND Request.Form(key) <> "" THEN
HasInput = TRUE
END IF
NEXT
NEXT
END FUNCTION
IF HasInput({"ih", "hdms"}) THEN
Use the Array() function:
If HasInput(Array("ih", "hdms")) Then
And to recieve the array:
Function HasInput(filters)
(though you can still use filters() if it makes it clearer that you're passing an array)

IsEmpty or IsNull or IsNothing?

Can someone help me determine which I should be using?
Here is the situation - I am pulling a value from a column in the Data Table. If there is anything in that column, I set the data to a variable and take an action. If the column is blank, I want to skip that.
I am confused as to which IsWHATEVER statement would be best. For Example:
If IsEmpty(Datatable.Value("M4","Data_Entry"))=False Then
OR
If IsNull(Datatable.Value("M4","Data_Entry"))=False Then
OR
If IsNothing(Datatable.Value("M4","Data_Entry"))=False Then
Suggestions?
I've just tried all of your options and found this to be the most correct:
If (DataTable.Value("M4","Global") <> "") Then
Your original options will not work on QTP Datatables as these are for uninitialised objects or variables. However, in QTP as soon as you create a parameter in the Datatable the first value gets initialised as blank (not to be confused with empty).
I agree with shreyansp.. The 3 options are for variables and objects
You could also use the below expression
If len(trim(DataTable.Value("M4","Global"))>0 Then
'Do code here
End If

Insert Property of Picture Class

I have read all of the questions on here about this topic and none of them provided me with a workable solution, so I'm asking this one.
I am running a legitimate copy of Excel 2013 in Windows 7. I record a macros where I insert a picture, and in the open file dialog I paste this URL: http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg (simply a picture of a product on Amazon). This works as expected.
The resulting macros looks like this:
Sub insertImage()
'
' percent Macro
'
'
ActiveSheet.Pictures.Insert( _
"http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg").Select
End Sub
However, when I attempt to run this, the Insert line breaks with the following error:
Run-time error '1004':
Unable to get the Insert property of the Picture class
I am trying to insert a number of pictures into an excel document and I am using the ActiveSheet.Pictures.Insert method to do this. I have been experiencing this issue there, so I recreated it in a way others could replicate to facilitate getting an answer...
An interesting thing to note is:
http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg 'This is what I pasted
http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg 'This is what the recorded macros recorded
It looks like Excel automatically resolved the %2B to a +. I tried making that change, but to no success.
Another interesting thing to note is that sometimes this does work and sometimes it doesn't. This url is a case where it does not work. Here's one where it does: http://ecx.images-amazon.com/images/I/51mXQ-IjigL._SL160_.jpg
Why would Excel generate a macros it can't run? More importantly, how can I avoid this error and get on with my work!? Thanks!
Try this workaround:
Sub RetrieveImage()
Dim wsht As Worksheet: Set wsht = ThisWorkbook.ActiveSheet
wsht.Shapes.AddPicture "http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg", _
msoFalse, msoTrue, 0, 0, 100, 100
End Sub
All fields are required, which is kind of a bummer since you cannot get the default size. The location offsets and sizes are in pixels/points. Also, the % turning to + is just alright, as % would cause it to be not recognized (dunno why).
Result:
Let us know if this helps.
I'm experiencing the same issue.
After some digging I found out Excel does a HTTP HEAD request before getting the image.
If this HEAD request is unsuccessful Excel will return the exact error messages mentioned in this discussion.
Your could easily test this using Fiddler.

Obtaining a reference to a NotesView with NotesDatabase.Views(x) can be surprisingly slow

In VB6, I'm supporting code that loops through all the views in a Lotus Notes database thusly:
For lngdomViewidx = LBound(domDatabase.Views) To UBound(domDatabase.Views)
Set domView = domDatabase.Views(lngdomViewidx) ' note: this line right here is slow to execute
The amount of time it takes to retrieve a reference to the view by index with this method seems proportional to the number of documents in the view. This bit of code is just looping through the Notes database to build a list of all the view names. On really large databases this can take several minutes. Is there a faster way to get this information?
Thanks!
That is not a very efficient way, no.
Use something like the code below instead. It is Lotusscript, but it should be pretty much the same in VB. I haven't tested it, just copied it from a production database and modified it to look for views instead for forms like in my original...
Dim ncol As NotesNoteCollection
Set ncol = db.CreateNoteCollection(True)
Call ncol.SelectAllNotes(False)
ncol.SelectViews = True
Call ncol.BuildCollection
noteID = ncol.GetFirstNoteId
For i = 1 To ncol.Count
Set doc = targetdb.GetDocumentByID(noteID)
MsgBox "view = " + doc.GetItemValue("$Title")(0)
noteID = ncol.GetNextNoteId(noteID)
Next
Karl-Henry is absolutely right: The NotesNoteCollection is really the fastest way to loop through all views.
But you can speed up your code significantly by just changing the loop.
Instead of opening each view using its index, you coud, do something like:
Forall view in db.Views
'Do whatever you want
End Forall
Accessing elements in a collection in Lotus Notes using the index means, that it always has to count from the beginning, while using a forall- loop directly accesses the next element...

Resources