Data Type issue while building notes database connections - lotus-notes

I fill the value of a specific notes document in a variable which has the data type VARIANT.
Reason: The value includes a backslash, letter and numbers.
Later in my code, I would like to build a database connections with this variable. Unfortunately it always fails with the following message:
Type mismatch in method CoerceString: Unkown found, Unkown expected
My code:
Dim varMailFile As Variant
Dim varMailServer As Variant
Dim maildb As New NotesDatabase( "", "" )
Dim cprofile As NotesDocument
vMailFile = doc.GetItemValue( "MailFile" )
vMailServer = doc.GetItemValue( "MailServer" )
Call maildb.Open(vMailServer, vMailFile)
I have already try to define the varMailFile and varMailServer as String, but it stilld doesnt work.
It makes also hard to troubleshoot, because the error message is not telling you what it found and what it expects.
I hope you can help me. Thanks.

NotesDocument.GetItemValue always returns a variant, even if the item only contains one value. You need EITHER to assign the first value (Index = 0 because LotusScript is 0- based by default) to your variable or just use the first value in your call:
First possibility:
varMailFile = doc.GetItemValue( "MailFile" )(0)
varMailServer = doc.GetItemValue( "MailServer" )(0)
...
Call maildb.Open(varMailServer, varMailFile)
Second possibility
varMailFile = doc.GetItemValue( "MailFile" )
varMailServer = doc.GetItemValue( "MailServer" )
...
Call maildb.Open(varMailServer(0), varMailFile(0))

Related

Getting Type Mismatch Error When Erasing Array

How do I get around this? The array is contained in a loop and needs to be cleared so that new values can be applied each run through the loop. I want to clear it before setting it. It is throwing the error the first run through when it contains no values... thank you
dim array as variant
erase array
array = ws.range("a1:z1000")
I also tried this but get the same error:
dim array as variant
if array <> empty then
erase array
end if
array = ws.range("a1:z1000")
The If statement is evaluating to True since it is treating empty as a variable. Use
If Not IsEmpty(array) then
instead.

How do I use Index & Match to reference a separate sheet?

Background:
I have the following test code that is working where column H is equal to what's in column B versus column D
Range("H2:H17") = "=INDEX(D2:D17,MATCH(B2:B17,B2:B17,0))"
Question:
How do I use this in code to reference a separate sheet called "temp" to do the same thing. The idea is for each time the code looks for 'target' it does
an index and match checking column B to equal what's in column D so if the valuue A is passed then it would become Test1?
I tried the following code, but target is not getting updated with any value.
Dim Target As Variant
With Application
Target = .Index(Sheets("Temp").Range("D2:D17"), .Match(Sheets("Temp").Range("B2:B17"), Sheets("Temp").Range("B2:B17"), 0))
End With
Debugging shows the following for Target
Any help is appreciated!
Match is a member of the WorksheetFunction interface; you need a WorksheetFunction object instance to invoke it off of - a With block could hold that object reference for you so you only need to type it once:
With Application.WorksheetFunction
Target = .Index(sheet.Range("D2:D17"), .Match(sheet.Range("B2:B17"), sheet.Range("B2:B17"), 0))
End With
Where sheet would be a Worksheet variable to work with, or a Worksheet parameter to your procedure.
Something looks wrong with the first argument to Match though: lookup_value wants to be a single value: the early-bound Application.WorksheetFunction.Match method is rather picky about what Variant subtypes it's willing to play along with nicely, and will throw a type mismatch error as-is.
The late-bound version (watch out for typos! Option Explicit can't save you from late-bound code!) works as expected with the range/array lookup value argument, and yields a Variant() array:
With Application
Target = .Index(sheet.Range("D2:D17"), .Match(sheet.Range("B2:B17"), sheet.Range("B2:B17"), 0))
End With
Make sure Target is a Variant, because this late-bound Match will yield a Variant/Error value if the lookup fails (the early-bound version raises a run-time error instead) - and that'll be a type mismatch error if you try to assign to anything but a Variant.

How to call a value of a variable whose name is passed as text to another variable?

I need to call a value of a variable as another variable. E.g.
I assign the FirstVariable = "One"
and then I asssign the Name as Text to
SecondVaribale = "FirstVariable" (Note here it is the "TEXT")
So now can I call or assign the SecondVariable to return the value as One in any ways?
Means this should return One:
Range("A1").Value = SecondVariable
is that possible?
Because I have about 40 such variables to be done in around 4 - 6 instances which I want to drive through a mapping sheet in Excel.
The easy way out is assigning the variables manually which would require manual intervention in future which I want to avoid.
You can create your own custom Dictionary or Collection in VBA for Excel 2007. Then you can "name" your variables, and use another string variable to indirectly access those "named variables". Choice of using Dictionary or Collection is how easy you need it to change the value of a "named variable".
A Dictionary allows you to add, read, change, and remove key/value pairs. A Collection only allows add, read, and remove; you have to use a subroutine to change a key/value pair. A Collection lets you use a numeric index (like an array) to access the key/value pairs; a Dictionary does not have an array-like feature. A pretty thorough comparison is at http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html
So to adapt your example, and to also show a change in value of a "named variable", here is some example code:
Public Function test() As String
' Dictionary example
Dim myDictionary, SecondVariable As String
Set myDictionary = CreateObject("scripting.dictionary")
myDictionary.Add "FirstVariable", "Four"
myDictionary.Add "AnotherVariable", "Two"
SecondVariable = "FirstVariable"
' note that "FirstVariable" must be already defined in the Dictionary else an error will occur; from your example this seemed to be the case
' if this was not the case then will need a more complex line using: If myDictionary.exists(SecondVariable) Then ... Else ...
myDictionary.Item(SecondVariable) = "One"
test = myDictionary.Item(SecondVariable) 'function returns "One"; the current value of "FirstVariable" in the Dictionary
End Function
Public Function test2() As String
' Collection example
Dim myCollection As New Collection, SecondVariable As String
myCollection.Add "Four", "FirstVariable"
myCollection.Add "Two", "AnotherVariable"
SecondVariable = "FirstVariable"
'myCollection(SecondVariable) = "One" 'Cannot do this with a Collection; have to use a Sub like the example below
Call setCollectionValue(myCollection, SecondVariable, "One")
test2 = myCollection(SecondVariable) 'function returns "One"; the current value of "FirstVariable" in the Collection
End Function
Private Sub setCollectionValue(collect As Collection, key As String, value As String)
On Error Resume Next
collect.Remove key
On Error GoTo 0
collect.Add value, key
End Sub

Can we add Null to the Array list?

please see the below, I am bit confused here:
Dim myQueue
Set myQueue= CreateObject("System.Collections.ArrayList")
myQueue.add("over")
myQueue.add("") 'is this valid statement
myQueue.add("") 'is this valid statement
Now Suppose an array(6)=(a,d,,,e,t)
For I=0 to Ubound(array)-1
myQueue.add(I)
Next
Now the Arraylist myQueue would contain the Non-Null values(i.e. a,d,e,t) or all the array element?
CODE I just tested and result is good for now
Option Explicit
Dim myQueue,i
Set myQueue = CreateObject("System.Collections.ArrayList")
'Dim Set Dic = CreateObject("Scripting.Dictionary
myQueue.Add("jumped")
myQueue.Add("over")
myQueue.Add("")
myQueue.Add("")
myQueue.Add("dog")
myQueue.Add("Cat")
i=0
Do While myQueue.Count > i
MsgBox(myQueue(i))
i=i+1
Loop
MsgBox("Capacity=" & myQueue.Capacity & "Count:" & myQueue.Count)
myQueue.Remove("dog")
MsgBox("Capacity=" & myQueue.Capacity & "Count:" & myQueue.Count)
i=0
Do While myQueue.Count > i
MsgBox(myQueue(i))
i=i+1
Loop
Thanks
By adding a null into the list, will you be validating for this null in the future? Meaning what's the purpose of adding the null?
If answer is yes, then you may add a special character, then validate for the special chracter that masks the null, and omit or take whatever action as it deems.
If answer is no, then yes you can pass a null into the list. It is not adding. Or just leave it empty.
PS:
AS PER MY COMMENT: It's great that if you could test out adding nulls into the arrayList to see whether you will be bugged by an error or not**

vba: This key is already associated with an element of this collection

Dim arr As New Collection, a
Dim aFirstArray() As Variant
...some code
For Each a In aFirstArray
arr.Add a, a
Next
i get this error: This key is already associated with an element of this collection
on this line : arr.Add a, a
what am i doing wrong?
Some more code?
from this what you have all I can tell you is that what your error says. The a is already in the collection (the key is supposed to be an unique string). BTW. is the a a string?
Or maybe you have some repetition in your array which would try to add 1+ time the same item?

Resources