VBA:Problem with DCOUNT + Problem while copying data from range to listbox

jonathan00243

New Member
Joined
May 26, 2019
Messages
1
Hi everyone,

I am having a hard time getting around some errors in my VBA code, so I was hoping to find some answers on this forum.

I have created a userform for creating new data that I can insert into an Access table called EMPLOYEE. While inserting the employee, I want to assign an Manager ID to each employee but the Manager ID must exist in the EMPLOYEE table, column EMPLOYEE_ID. Here is the code that I have used for this purpose:

Code:
  Private sub create_click ()
Dim conn as object
Dim rs as Object
Dim strconn as String
Dim qry as String
Dim msg

Set conn = createobject("ADODB.connection")
set rs = createobject("ADODB.recordset")

strconn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data source = C:\MyPath\MyDB.accdb"
qry = "select * from employee"

with rs
.adnew
if isnumeric(ManagerID) = false then
msgbox "Invalid Manager ID"
exit sub
elseif application.worksheetfunction.dcount("employee_ID","employee","activ='Yes' and employee_ID='" & [EmployeeForm.ManagerID] & "'") = 0 then
msgbox "Manager ID does not exist "
exit sub
else
. fields("Manager_ID").value = ManagerID
end if
end with

rs.update
with employee
.superviseurID.value = ""
rs.close
set rs = nothing
conn.close
set conn = nothing

msgbox "Operation completed"
end sub ()

When I run this code I get the error : 'Compile error Type' with Employee_ID highlighted in the Dcount line.

I have looked through the internet and I understand this might be due to the fact that the source field ManagerID in the userform is a textbox while the target field Manager_ID in the Access table is a number but I have tried every solution found on forums but nothing is working, hence my presence in this forum :). Can someone help figure out what the problem is here please?


The second problem that I am encountering is when retrieving data from Access, copying it into an excel sheet and showing the result into a Listbox.

In the same userform, I have a search button that looks through the Employee table to find data. I am able to copy the data into a dynamic range in excel but when I try to show the result in the ListBox, only the first column is being shows. Here is how I created my dynamic range:

Code:
OutputSource= OFFSET(EMPLOYEE!$A$1;0;0;COUNTA(EMPLOYEE!$A:$A),19)

And here is the code that I have created:

Code:
Private sub search_click ()
Dim conn as object
Dim rs as Object
Dim strconn as String
Dim qry as String
Dim var1

Set conn = createobject("ADODB.connection")
set rs = createobject("ADODB.recordset")

strconn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data source = C:\MyPath\MyDB.accdb"

var1 = me.employeeid

if activ = true then
qry = "select * from employee where activ = 'Yes' and employeeid=" & var1
else 
qry = "select * from employee where  employeeid=" & var1
end if

application.Screenupdating = false
thisworkbook.worksheets("employee").range("a2:s10000").clearcontents

conn.open(strconn)
rs.open qry, conn

if rs.eof and rs.bof then
rs.close
conn.close
set rs = nothing
set conn= nothing
application.Screenupdating = true
msgbox "No result"
exit sub
end if
thisworkbook.worksheets("employee").range("a2").copyfromrecordset rs

rs.close
conn.close
set rs = nothing
set conn = nothing

application.screenupdating = true

me.listbox1.rowsources = "OutputSource"

end sub

Can someone help me figure out what I am doing wrong here?

Thanks :)
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top