Create class method with already existing name from a DLL - vba

I come from this post : Create class method with already existing name, where I explain that I need to make code compatible with MS Access and PostgreSQL. My first thought was to create a new class for PostgreSQL support and rewrite the functions previously used. The solution brought could work, but the problem is that the MS Access database class is loaded from a DLL (Microsoft Shared\DAO\dao360.dll, DAO.DBEngine). How can I interface or override those DLL functions and get rid of this "ambiguous name detected" error ?

As said in the answer to your first post you can use the Implements feature to do this. Without further details and yout code it is just more than difficulut to tell where your issue is.
IDatabase could look like that
Option Explicit
Sub OpenDB(fileName As String)
End Sub
Function ReadData(lngNr As Long) As String
End Function
clsAccess like that
Option Explicit
Implements IDatabase
Dim m_Dbs As DAO.Database
Dim m_Rcd As DAO.Recordset
Dim m_Filename As String
Sub IDatabase_OpenDB(fileName As String)
m_Filename = fileName
Set m_Filename = OpenDatabase(m_Filename, , True)
End Sub
Function IDatabase_ReadData(lngNr As Long) As String
Set m_Rcd = m_dbs.OpenRecordset("qryTable", dbOpenSnapshot)
m_Rcd.Move lngNr - 1
IDatabase_ReadData = m_Rcd.Fields("fldName").Value
m_Rcd.Close
End Function
You need to add the references in Tools/References.

Related

Using MSscriptControl in VB.net adding an object gives Specified cast is not valid error

Here a sample code that produces the error. I have used MSscript in VB projects in the past and those projects are functioning.
The error reported is: "When casting from a number, the value must be a number less than infinity"
Or if anyone has another suggested way to easily add scripting to a project.
Private Sub Run_Script()
Dim scriptEngine As New MSScriptControl.ScriptControl()
Dim TestClass As New Sample
Dim ScriptCode As String
scriptEngine.Language = "VbScript"
scriptEngine.AddObject("Test", TestClass, True)
ScriptCode = "MsgBox ""tests"" "
scriptEngine.AddCode(ScriptCode)
End Sub
End Class
Public Class Sample
Public Sub Test()
MessageBox.Show("This is a test")
End Sub
End Class
I found the answer. I needed to set com visible to true. This is found under "Assembly Information"

How Do I fill in this Structure String Array in vbNET?

Visual studio tells me the variable must be declared even though it already is.
I filled in a structured array in a similar way using a loop though the type was an Int.
I do not want to use a loop this time just hard code it.
Structure Sentence
Dim strWord As String
End Structure
Dim strArticles(1) As Sentence
strArticles(0).strWord = "The"
Thanks
Are you defining the Structure in your method body? It must be defined outside of a method, either in a module ore a class. See this example.
This works just fine:
Module Module1
Sub Main()
Dim s = New Sample()
s.DoIt()
End Sub
End Module
Class Sample
Structure Sentence
Dim strWord As String
End Structure
Public Sub DoIt()
Dim strArticles(1) As Sentence
strArticles(0).strWord = "The"
Console.WriteLine(strArticles(0).strWord)
End Sub
End Class

Download URL Contents Directly into String (VB6) WITHOUT Saving to Disk

Basically, I want to download the contents of a particular URL (basically, just HTML codes in the form of a String) into my VB6 String variable. However, there are some conditions.
I know about the URLDownloadToFile Function - however, this requires that you save the downloaded file/HTML onto a file location on disk before you can read it into a String variable, this is not an option for me and I do not want to do this.
The other thing is, if I need to use an external library, it must already come with all versions of Windows from XP and onwards, I cannot use a control or library that I am required to ship, package and distribute even if it is free, this is not an option and I do not want to do this. So, I cannot use the MSINET.OCX (Internet Transfer) Control's .OpenURL() function (which simply returns contents into a String), as it does not come with Windows.
Is there a way to be able to do this with the Windows API, URLMON or something else that is pre-loaded into or comes with Windows, or a way to do it in VB6 (SP6) entirely?
If so, I would appreciate direction, because even after one hour of googling, the only examples I've found are references to URLDownloadToFile (which requires saving on disk before being ale to place into a String) and MsInet.OpenURL (which requires that I ship and distribute MSINET.OCX, which I cannot and don't want to do).
Surely there has got to be an elegant way to be able to do this? I can do it in VB.NET without an issue, but obviously don't have the luxury of the .NET framework in VB6 - any ideas?
Update:
I have found this: http://www.freevbcode.com/ShowCode.asp?ID=1252
however it says that the displayed function may not return the entire
page and links to a Microsoft bug report or kb article explaining
this. Also, I understand this is based off wininet.dll - and I'm
wondering which versions of Windows does WinInet.dll come packaged
with? Windows XP & beyond? Does it come with Windows 7 and/or Windows
8?
This is how I did it with VB6 a few years ago:
Private Function GetHTMLSource(ByVal sURL As String) As String
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.XmlHttp")
xmlHttp.Open "GET", sURL, False
xmlHttp.send
GetHTMLSource = xmlHttp.responseText
Set xmlHttp = Nothing
End Function
If you want to do this with pure VB, and no IE, then you can take advantage of a little-used features of the VB UserControl - async properties.
Create a new UserControl, and call it something like UrlDownloader. Set the InvisibleAtRuntime property to True. Add the following code to it:
Option Explicit
Private Const m_ksProp_Data As String = "Data"
Private m_bAsync As Boolean
Private m_sURL As String
Public Event AsyncReadProgress(ByRef the_abytData() As Byte)
Public Event AsyncReadComplete(ByRef the_abytData() As Byte)
Public Property Let Async(ByVal the_bValue As Boolean)
m_bAsync = the_bValue
End Property
Public Property Get Async() As Boolean
Async = m_bAsync
End Property
Public Property Let URL(ByVal the_sValue As String)
m_sURL = the_sValue
End Property
Public Property Get URL() As String
URL = m_sURL
End Property
Public Sub Download()
UserControl.AsyncRead m_sURL, vbAsyncTypeByteArray, m_ksProp_Data, IIf(m_bAsync, 0&, vbAsyncReadSynchronousDownload)
End Sub
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
If AsyncProp.PropertyName = m_ksProp_Data Then
RaiseEvent AsyncReadComplete(AsyncProp.Value)
End If
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
If AsyncProp.PropertyName = m_ksProp_Data Then
Select Case AsyncProp.StatusCode
Case vbAsyncStatusCodeBeginDownloadData, vbAsyncStatusCodeDownloadingData, vbAsyncStatusCodeEndDownloadData
RaiseEvent AsyncReadProgress(AsyncProp.Value)
End Select
End If
End Sub
To use this control, stick it on a form and use the following code:
Option Explicit
Private Sub Command1_Click()
XDownload1.Async = False
XDownload1.URL = "http://www.google.co.uk"
XDownload1.Download
End Sub
Private Sub XDownload1_AsyncReadProgress(the_abytData() As Byte)
Debug.Print StrConv(the_abytData(), vbUnicode)
End Sub
Suffice to say, you can customise this to your hearts content. It can tell (using the AyncProp object) whether the file is cached, and other useful information. It even has a special mode in which you can download GIF, JPG and BMP files and return them as a StdPicture object!
One alternative is using Internet Explorer.
Dim ex As InternetExplorer
Dim hd As HTMLDocument
Dim s As String
Set ex = New InternetExplorer
With ex
.Navigate "http://donttrack.us/"
.Visible = 1
Set hd = .Document
s = hd.body.innerText ' assuming you just want the text
's = hd.body.innerHTML ' if you want the HTML
End With
EDIT: For the above early binding to work you need to set references to "Microsoft Internet Controls" and "Microsoft HTML Object Library" (Tools > References). You could also use late binding, but to be honest, I forget what the proper class names are; maybe someone smart will edit this answer :-)

Trying to trouble Object required error in VBA

I got this problem. I have a form that retrieves a table data using the forms' record source property. When the form's opened, I set its record source property to a module's public method RetrieveMembers. Here's the code below.
Private Sub Form_Open(Cancel As Integer)
'set Form's record source property to retrieve a Members table
Me.RecordSource = mod_JoinMember.RetrieveMembers
End Sub
'mod_JoinMember Class
Public Function RetrieveMembers() As String
Dim strSQL As String
Set strSQL = "SELECT tbl_Member.Title, tbl_Member.Gender, tbl_Member.LastName,
tbl_Member.DateofBirth, tbl_Member.Occupation, tbl_Member.PhoneNoWork,
tbl_Member.PhoneNoHome, tbl_Member.MobileNo, tbl_Member.Email,
tbl_Member.Address, tbl_Member.State, tbl_Member.Postcode FROM tbl_Member;"
RetrieveMembers = strSQL
End Function
Object required error is thrown.
I couldn't comprehend this compile error. I see no wrong with my code since recordsource is a String type property. And my module's function Retrievemembers is returning a String value.
Why is it that it's not satisfied with this?
Thanks for your help.
I fixed it. The reason was because String is not really an Object to begin with. So the 'Set' keyword is not needed - since you don't need to explicitly declare String-type objects anyway!
All good now!
As you are working with a class module I think you will need to use:
Public Property Get RetrieveMembers() As String
Rather than:
Public Function RetrieveMembers() As String

Dynamic properties for classes in visual basic

I am a vb.net newbie, so please bear with me. Is it possible to create properties (or attributes) for a class in visual basic (I am using Visual Basic 2005) ? All web searches for metaprogramming led me nowhere. Here is an example to clarify what I mean.
public class GenericProps
public sub new()
' ???
end sub
public sub addProp(byval propname as string)
' ???
end sub
end class
sub main()
dim gp as GenericProps = New GenericProps()
gp.addProp("foo")
gp.foo = "Bar" ' we can assume the type of the property as string for now
console.writeln("New property = " & gp.foo)
end sub
So is it possible to define the function addProp ?
Thanks!
Amit
It's not possible to modify a class at runtime with new properties1. VB.Net is a static language in the sense that it cannot modify it's defined classes at runtime. You can simulate what you're looking for though with a property bag.
Class Foo
Private _map as New Dictionary(Of String, Object)
Public Sub AddProperty(name as String, value as Object)
_map(name) = value
End Sub
Public Function GetProperty(name as String) as Object
return _map(name)
End Function
End Class
It doesn't allow direct access in the form of myFoo.Bar but you can call myFoo.GetProperty("Bar").
1 I believe it may be possible with the profiling APIs but it's likely not what you're looking for.
Came across this wondering the same thing for Visual Basic 2008.
The property bag will do me for now until I can migrate to Visual Basic 2010:
http://blogs.msdn.com/vbteam/archive/2010/01/20/fun-with-dynamic-objects-doug-rothaus.aspx
No - that's not possible. You'd need a Ruby like "method_missing" to handle the unknown .Foo call. I believe C# 4 promises to offer something along these lines.