# VB6 DHTML - Expamples and help please!



## hey yer barred (Aug 5, 2008)

Hi All

I've been google-ing for well over a hour now and have found nothing to help me.

I've been using VB6 now for well over 3 years and never ever used the DHTML enviorment, and being as i have some time....im going to have a gander at it!

I have a select box that i have no idea how to add items to it from a recordset. Normally i would do this

If not(rs.eof) then
do while (not(rs.eof))
me.list1.additem = rs!Name
rs.movenext
loop
end if

This doesnt work on the html select box...can anyone help me please?

Also, can someone please post some likes to any good tutrials for this.

Your help is appresicated!


----------



## hey yer barred (Aug 6, 2008)

that or if anyone has a example of VB code running in HTML please.


----------



## RoryA (Aug 6, 2008)

Here's a sample of an hta I use for getting WMI class information:

```
<head>
<title>HTA Test</title>
<HTA:APPLICATION 
     APPLICATIONNAME="HTA Test"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="normal"
>
</head>
<script language="VBScript">
Sub FillClassDropDown
   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}root\cimv2")
   Set colClasses = objWMIService.SubClassesOf
   
   For Each objClass In colClasses
      If Left(objClass.Path_.Class,6) = "Win32_" Then
         Set objNewOption = document.createElement("OPTION")
         objNewOption.Text = objClass.Path_.Class
         WMIClasses.options.Add(objNewOption)
      End If
   Next
 
End Sub
Sub HandleClassChange
   For Each objOption In WMIClasses.Options
      If objOption.Selected = True Then
      strClass = objOption.Text
      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}root\cimv2")
      Set objClass = objWMIService.Get(strClass)
      For Each objProperty In objClass.Properties_
          strProperties = strProperties & objProperty.Name & vbCrLf
      Next
   
      For Each objMethod In objClass.Methods_
         strMethods = strMethods & objMethod.Name & vbCrLf
      Next
      Properties.Value = strProperties
      Methods.Value    = strMethods
      End If
   Next
End Sub
Sub ShowCode
For Each objOption In WMIClasses.Options
      If objOption.Selected = True Then
     strClass = objOption.Text
  End If
Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}root\cimv2")
Set objClass = objWMIService.Get(strClass)
strCode = strCode & "Set objWMIService = GetObject(""winmgmts:"")" & vbCrLf
strCode = strCode & "Set colInstances = objWMIService.ExecQuery" & _
    "(""SELECT * FROM " & strClass & """)" & vbCrLf
strCode = strCode & "For Each objInstance In colInstances" & vbCrLf
For Each objProperty In objClass.Properties_
   strCode = strCode & "   WScript.Echo objInstance." & objProperty.Name & vbCrLf
Next
strCode = strCode & "Next" 
Code.Value = strCode
End Sub
</script>
<body onLoad=FillClassDropDown background="Lakka.jpg">
<select onChange=HandleClassChange name="WMIClasses"></select><p> 
<input onClick=ShowCode type="submit" value="Show Code"></input><p>
<textarea name="Properties" rows="5" cols="45"></textarea>
<textarea name="Methods" rows="5" cols="45"></textarea>
<textarea name="Code" rows="30" cols="93"></textarea>
</body>
</html>
```
if that helps. (the script tags should interest you!)


----------



## hey yer barred (Aug 6, 2008)

thanks rorya....this looks like it will help me alot!


----------



## hey yer barred (Aug 6, 2008)

how would i add references into this?

in VB6 i would go on project, then references


----------



## RoryA (Aug 6, 2008)

I don't think you can. You're using VBScript rather than VB, so variables have no type and you late bind everything. You use CreateObject to instantiate new objects, rather than the New keyword.


----------



## hey yer barred (Aug 6, 2008)

do you have anyidea then how i would make a ADODB connection then please?
Heres what i would normally code

Dim db As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sPath, sSQL, sconn As String

sPath = App.Path & "Test.mdb"
sconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath
db.ConnectionString = sconn
db.Open
With rs
    .ActiveConnection = db
    .LockType = adLockOptimistic
    .CursorType = adOpenKeyset
    .Open "SELECT * FROM tblMain"
End With

End Sub


----------



## RoryA (Aug 6, 2008)

```
Dim db
Dim rs
set db = CreateObject("ADODB.Connection")
set rs = CreateObject("ADODB.Recordset")
... etc
```
Note you will have to use the literal value for constants like adLockOptimistic.


----------



## hey yer barred (Aug 6, 2008)

rorya, your help as been fantastic!
very much appreaciated!


----------



## RoryA (Aug 6, 2008)

Glad to help.


----------



## hey yer barred (Aug 5, 2008)

Hi All

I've been google-ing for well over a hour now and have found nothing to help me.

I've been using VB6 now for well over 3 years and never ever used the DHTML enviorment, and being as i have some time....im going to have a gander at it!

I have a select box that i have no idea how to add items to it from a recordset. Normally i would do this

If not(rs.eof) then
do while (not(rs.eof))
me.list1.additem = rs!Name
rs.movenext
loop
end if

This doesnt work on the html select box...can anyone help me please?

Also, can someone please post some likes to any good tutrials for this.

Your help is appresicated!


----------



## hey yer barred (Aug 6, 2008)

sorry....just one more thing...
what do i do when it comes to the adLockOptimistic


----------



## RoryA (Aug 6, 2008)

You have to either declare the variables and assign their values, or just use the values - so instead of adLockOptimistic you would put 3.


----------



## hey yer barred (Aug 6, 2008)

i hope im as clever as you are once day rorya!


----------



## hey yer barred (Aug 7, 2008)

sorry to bring this up again....im having a problem with my SQL statement, very simple query

rs.open "SELECT * FROM tblName"

it wont run, anyone got anyideas?


----------



## hey yer barred (Aug 7, 2008)

sorry, the error is 'Expected end of statement'


----------



## RoryA (Aug 7, 2008)

You seem to be missing the connection part of that, or have you set it elsewhere?


----------



## hey yer barred (Aug 7, 2008)

heres my code, i should have just posteed this in the first place!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<HEAD>
<TITLE>VB6 Test HTML</TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<script language="VBScript">

sub TestDBConnect
Dim db
Dim rs
set db = CreateObject("ADODB.Connection")
set rs = CreateObject("ADODB.Recordset")

sPath = App.Path & "\TestDB.mdb"
sconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath
db.ConnectionString = sconn
db.Open

rs.ActiveConnection = db
rs.LockType = 3
rs.CursorType = 1
rs.Open "SELECT * FROM tblManager"

msgbox rs!Name
rs.close
db.close
End sub

</script>

<META content="MSHTML 6.00.2900.3268" name=GENERATOR>
</HEAD>
<BODY>
<INPUT onclick=TestDBConnect type="button" value="Button">
</BODY>
</html>


----------



## RoryA (Aug 7, 2008)

You need to change this:

```
msgbox rs!Name
```
to this:

```
msgbox rs.Fields("Name")
```
or just:

```
msgbox rs("Name")
```


----------



## hey yer barred (Aug 7, 2008)

i cannot thank you enough rorya! your a legend, thnak you!


----------



## RoryA (Aug 7, 2008)

No worries.


----------



## hey yer barred (Aug 5, 2008)

Hi All

I've been google-ing for well over a hour now and have found nothing to help me.

I've been using VB6 now for well over 3 years and never ever used the DHTML enviorment, and being as i have some time....im going to have a gander at it!

I have a select box that i have no idea how to add items to it from a recordset. Normally i would do this

If not(rs.eof) then
do while (not(rs.eof))
me.list1.additem = rs!Name
rs.movenext
loop
end if

This doesnt work on the html select box...can anyone help me please?

Also, can someone please post some likes to any good tutrials for this.

Your help is appresicated!


----------



## hey yer barred (Aug 21, 2008)

sorry to bring this up again...ihow do i create a table on the website from the database?

i have this coding, but it doesnt work

if not (rs.EOF) then
	do while (not(rs.EOF))
	write "<TABLE cellSpacing=0 cellPadding=0 width=700 bgColor=white border=1>"
	write "<TR>"
	write "<TD>" & rs.Fields("Name")
	write "</TD>"
	loop
end if
write "</TR>"
write "</TABLE>"
rs.close
db.close

come up with a error saying Type Mismatch: 'write'


----------



## RoryA (Aug 21, 2008)

Absolutely no idea, I'm afraid. (D)HTML is not my area of expertise.


----------



## hey yer barred (Aug 21, 2008)

not to worry...ive worked it out...

document.write "<td>" etc.....


----------

