I want to modify my function that strips the tag from the value of a database record in the title of French pages:
Function removeSup(strToRemove)
strToRemove = Replace(strToRemove,"<sup>","(")
strToRemove = Replace(strToRemove,"</sup>",")")
response.Write(strToRemove)
End Function
so that it can read the contents of the tag. If the contents is a ® or a ™, then do nothing, but if it's MD or MC, then do the replace.
I just haven't figured out how to read strings with VBScript so is this something that I should do with VBScript or have it passed to jQuery to do it?
You could work with Regular Expressions but if i understand what you mean the you can do it as below.
Jquery is javascript only, if this is for web, best not to mix vbscript and javascript unless realy necessary.
Function removeSup(strToRemove)
strToRemove = Replace(strToRemove,"<sup>MD</sup>","(MD)")
strToRemove = Replace(strToRemove,"<sup>MC</sup>","(MD)")
response.Write(strToRemove)
End Function
Related
I'm trying to get Word to fill in cells in a table. The script works when run as a macro from within Word, but fails when saved as a .vbs file and double-clicked, or run with wscript. This is a part of it.
set obj = GetObject(,"Word.Application)
With obj
With .Selection
MsgBox .text
If (.Information(wdWithInTable) = True) Then
.Collapse Direction:=wdCollapseStart
tCols = .Tables(1).Columns.Count
tRow = .Information(wdStartOfRangeRowNumber)
tCol = .Information(wdStartOfRangeColumnNumber)
For I = 2 To 5
.Tables(1).Cell(tRow, I).Range.Text = "fred" & Str(I)
Next
` now make new row
For I = 1 To tCols - tCol + 1
.MoveRight unit:=wdCell
Next
End If
End With
End With
I have three problems. First, it won't compile unless I comment out the .Collapse and .MoveRight lines. Second, although the MsgBox .text displays the selected text, I get "out of range" errors if I try to access any .Information property.
I'm sure I'm missing something very simple: I usually write software for Macs, and I'd do this using AppleScript. This is my first attempt at getting anything done under Windows.
VBScript and VBA are different languages.
They are a bit similar, but not very. Moreover, VBScript is not like AppleScript; it doesn't let you easily interface with running programs.
The interfaces you'll get from VBScript can behave subtly differently in VBA and VBScript. However, I think you've got two problems here:
:= is invalid syntax in VBScript; you'll need to find an alternative way of calling the function. Try just using positional arguments.
You've no guarantee that this will open the expected file; there could be another instance of Word that it's interacting with instead.
Since your code is not running within the Word environment it would require a reference to the Word object library in order to use enumeration constants (those things that start with wd).
VBScript, however, cannot work with references, which means the only possibility is to use the long value equivalents of the enumerations. You'll find these in the Word Language References. Simplest to use is probably the Object Browser in Word's VBA Editor. (In Word: Alt+F11 to open the VBA Editor; F2 to start the Object Browser; type in the term in the "Search" box, click on the term, then look in the bottom bar.)
The code in the question uses, for example:
wdWithInTable
wdCollapseStart
wdStartOfRangeRowNumber
wdStartOfRangeColumnNumber
wdCell
The reason you get various kinds of errors depends on where these are used.
Also, VBScript can't used named parameters such as Unit:=. Any parameters must be passed in comma-delimited format, if there's more than one, in the order specified by the method or property. If there are optional parameters you don't want to use these should be left "blank":
MethodName parameter, parameter, , , parameter
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.
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)
Here is the situation, I have this string table, in a .res file and I have some strings loaded into one of the forms, say Form1. On a form on I want to popup a message box with a message loaded from the string table using LoadResString(1234).
Is it possible that when Resource ID 1234 contains "This is testing vbNewline This is a new line!." that string will be loaded using the said function onto the message box (popup box)?
I've tested it: It will also print out the "vbNewline" command and NOT parse it. Is there any other way to parse the said string making this kind of message ?
This is testing
This is new line!.
I wanted that kind of message to appear.
You are trying to put a VB constant in a String expression so it is treating it like text, you can try using the Replace Function ( I realize this is a .Net link but the signature is the same as the VB6 method) to remove your string and substitute the correct value something like this should work:
MsgBox (Replace(LoadResString(1234), "vbNewLine", vbNewLine))
or create a function like this:
Public Function ParseNewLine(value As String) As String
ParseNewLine = Replace(value, "vbNewLine", vbNewLine)
End Function
and call it like this:
MsgBox (ParseNewLine(LoadResString(1234)))
Why don't you embed the newline sequence into the RES file. If you are using the Resource Add-In, you can press Ctrl+Enter to insert these characters.
In visual Studio's resource editor and a raw resource script, you can use \n.
I am new to VB. I am testing some old VB code. The code is as follows -
Public GlobalCommArea() As Byte
...
...
'GlobalCommArea is set to some value
Now, I want to see the contents of this GlobalCommArea variable. (By the way, is it a variable?)
So I tried
outputBox.Text = GlobalCommArea
But the outputBox (which is a textbox) didn't show anything. What should I do to print the contents of GlobalCommArea into the textbox?
The Byte data type is an array of bytes actually. You need to convert it to a string.
Use this to convert it:
outputBox.Text = StrConv(GlobalCommArea, vbUnicode)
Depending on what's stored in GlobalCommArea you may have to change the vbUnicode parameter.
Hope this helps